abstract JInternalFrame

I'm a fairly new programmer and I have an assignment to make for school; It's a program which uses a Swing Gui and the Gui uses a lot of JInternal Frames. Now, in the initial stage I made all the classes and I noticed that all of the classes that use JInternalFrames have some stuff in common like fo instance all of the attributes of the JInternalFrame for instance the DefaultCloseOperation and the icon at the top. Would it be a good idea to make an abstract class that extends from JInternalFrame and already has all these properties set? Or is there another way to do this? Hope anyone can help me.

[612 byte] By [Rockarena666a] at [2007-11-27 2:59:52]
# 1

I don't see a need to subclass -- are you overriding a method? Instead, what about a configure method that you could add to the class that creates your GUI:

void configure(JInternalFrame iFrame, other params?)

Or even:

JInternalFrame createInternalFrame(params?)

... if creating the JInternalFrame is the same in every case.

Message was edited by:

DrLaszloJamf

DrLaszloJamfa at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 2

Ah, so no need to subclass. That will save me a lot of work I guess but I'm not sure I completely understand your solution.

In my case I have to write an application for a Doctors practice. I have classes like changePatientData. That class would use a JInternalFrame to display the data.

Code of a classdeclaration:public class changePatientData extends JInternalFrame

I have several others of these classes that all extend JInternalFrame in the same way. How would I implement your solution then? Sorry but I don't have that much of experience yet programming java and GUI's but I'd be grateful if you could clarify your solution.

Message was edited by:

Rockarena666

Rockarena666a at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 3
Tell you want: post a bit of a JInternalFrame subclass, and I will show you how to get rind of the subclassing.
DrLaszloJamfa at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 4

Some of the stuff will be in Dutch so sorry about that and really thank you for the help!

/*

* Toevoegen.java

*

* Created on 3 januari 2007, 20:47

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package forms;

import ejb.*;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.util.List;

import java.util.Vector;

import javax.swing.*;

import java.awt.GridLayout;

import java.awt.event.*;

import javax.ejb.EJB;

import login.Login;

import login.LoginGegevens;

public class Toevoegen extends JInternalFrame implements MouseListener

{

public SuperFacadeRemote superfacadebean = Login.getSuperFacade();

public Container container;

public LoginGegevens logingeg;

public JPanel panel;

private JTextField txtNaam, txtVoornaam, txtEmail, txtTelnr, txtGsmnr, txtStraat, txtGemeente, txtPostcode, txtHuisnr, txtLand;

protected JButton therapeut, patient, arts,hoofdarts;

private JCheckBox mails;

private JLabel label;

protected JLabel foutlabel;

private JComboBox comboarts;

protected JPasswordField txtPaswoord,txtPaswoord2;

private JPanel titel;

protected String persoon;

private List listarts;

protected Persoon p;

private JList therapat;

public Toevoegen(LoginGegevens logingeg, String persoon)

{

super();

this.logingeg = logingeg;

this.persoon=persoon;

container = new Container();

container = getContentPane();

container.setLayout(new BorderLayout());

aanmakenKnoppen();

setPanel();

bewerkPersoon();

foutlabel = new JLabel("");

foutlabel.setForeground(Color.RED);

container.add(foutlabel, BorderLayout.SOUTH);

this.setMaximizable(true);

this.setClosable(true);

this.setResizable(true);

this.setLocation(0,0);

this.setSize(400,400);

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

this.setVisible(true);

stresslabo.HoofdForm.dskp.add(this);

stresslabo.HoofdForm.dskp.moveToFront(this);

}

public void aanmakenKnoppen()//String persoon)

{

if (persoon.equals ("therapeut"))

{

therapeut = new JButton("Voeg toe");

therapeut.addMouseListener(this);

}

else

{

if(persoon.equals ("arts"))

{

arts = new JButton("Voeg toe");

arts.addMouseListener(this);

}

else

{

if(persoon.equals ("patient"))

{

patient = new JButton("Voeg toe");

patient.addMouseListener(this);

}

else

{

hoofdarts = new JButton("Voeg toe");

hoofdarts.addMouseListener(this);

}

}

}

/*therapeut = new JButton("Voeg toe");

therapeut.addMouseListener(this);

patient = new JButton("Voeg toe");

patient.addMouseListener(this);

arts = new JButton("Voeg toe");

arts.addMouseListener(this);*/

}

void setKnopteksten(String s)

{

if (persoon.equals ("therapeut"))

therapeut.setText(s);

else

{

if(persoon.equals ("arts"))

arts.setText(s);

else

{

if(persoon.equals ("patient"))

patient.setText(s);

else

hoofdarts.setText(s);

}

}

}

public void setPanel()//JPanel panel)

{

panel = new JPanel();

//panel.removeAll();

GridLayout gl = new GridLayout (16,2);

gl.setHgap(1);

gl.setVgap(1);

panel.setLayout(gl);

}

public void setTitel(String t)

{

titel = new JPanel();

titel.add(new JLabel(t));

container.add(titel, BorderLayout.NORTH);

}

public void bewerkPersoon ()

{

setTitel(persoon.toUpperCase() + " TOEVOEGEN");

persoonVelden();

if (persoon.equals("patient"))

addPatient();

paswoordVelden();

//leegVeld();

knopLeegmaken();

if(persoon.equals("therapeut"))

{

panel.add(therapeut);

}

else

{

if (persoon.equals("arts"))

panel.add(arts);

else

{

if (persoon.equals("patient"))

panel.add(patient);

else

panel.add(hoofdarts);

}

}

container.add(panel, BorderLayout.CENTER);

}

public void knopLeegmaken()

{

JButton leegmaken=new JButton("Velden leegmaken");

leegmaken.addMouseListener(new MouseAdapter() {

public void mouseReleased(MouseEvent e)

{

maakLeeg();

}

});

panel.add(leegmaken);

}

public void addPatient ()

{

label = new JLabel("Arts:");

panel.add(label);

/*-Lijsten aanmaken-*/

List<Arts> listarts = superfacadebean.geefArtsen(logingeg.getLogin(), logingeg.getPaswoord());

/*Test lijsten maken -*

listarts=maakList("arts");

/*-Einde TEST*/

comboarts = new JComboBox((Vector)listarts);

panel.add(comboarts);

}

public void addTherapeut()

{

panel.add(new JLabel("Pati雗ten:"));

therapat = new JList();

panel.add(therapat);

}

public void addConsultaties()

{

panel.add(new JLabel(""));

JButton consknop = new JButton("Consultaties");

consknop.addMouseListener(new MouseAdapter() {

public void mouseReleased(MouseEvent e)

{

new Consultaties(p);

}

});

panel.add(consknop);

}

/*TEST LIJSTEN */

public List maakList(String pers)

{

listarts = new Vector ();

Arts a=new Arts("aArts", "Jandslkjq", "arts@mail.com", "001122", "01475", true, new Adres ());

if (pers.equals("arts"))

{

listarts.add(new Arts("Verheyden", "Jan", "jan@mail.com", "001122", "01475", true, new Adres ()));

listarts.add(new Arts("Janssen", "Jandslkjq", "ja2n@mail.com", "00551122", "0142475", false, new Adres ()));

listarts.add(a);

}

else

{

if (pers.equals("therapeut"))

{

listarts.add(new Therapeut("Pieters", "Peter", "ja3@mail.com", "0014422", "0123475", true, new Adres ()));

listarts.add(new Therapeut("Thera", "Peut", "theaja3@mail.com", "110014422", "220123475", false, new Adres ()));

listarts.add(new Therapeut("Prens", "PTherapeutr", "japeut3@mail.com", "001244422", "023123475", true, new Adres ()));

}

else

{

listarts.add(new Patient("Patient", "odsqfs", "patient@mail.com", "00dfgsdf1122", "04561475", false, new Adres (),a));

listarts.add(new Patient("Patient2", "twee", "p22atient@mail.com", "0220dfgsdf1122", "0224561475", true, new Adres (),a));

//superfacadebean.geefPersonen();

}

}

return listarts;

}

/*EINDE TEST */

public void leegVeld()//JPanel panel)

{

label = new JLabel("");

panel.add(label);

}

public void persoonVelden()//JPanel panel)

{

label = new JLabel("Naam:");

panel.add(label);

txtNaam = new JTextField();

panel.add(txtNaam);

label = new JLabel("Voornaam:");

panel.add(label);

txtVoornaam = new JTextField();

panel.add(txtVoornaam);

if (!(persoon.equals("hoofdarts")))

{

adresVelden();//panel);

label = new JLabel("Email:");

panel.add(label);

txtEmail = new JTextField();

panel.add(txtEmail);

label = new JLabel("Telefoonnr:");

panel.add(label);

txtTelnr = new JTextField();

panel.add(txtTelnr);

label = new JLabel("GSMnr:");

panel.add(label);

txtGsmnr = new JTextField();

panel.add(txtGsmnr);

leegVeld();//panel);

mails = new JCheckBox("Emails ontvangen", true);

panel.add(mails);

}

}

public void paswoordVelden ()//JPanel panel)

{

label = new JLabel("Paswoord:");

panel.add(label);

txtPaswoord = new JPasswordField();

panel.add(txtPaswoord);

label = new JLabel("Herhaal paswoord:");

panel.add(label);

txtPaswoord2 = new JPasswordField();

panel.add(txtPaswoord2);

}

public void adresVelden ()//JPanel panel)

{

label = new JLabel("Straat:");

panel.add(label);

txtStraat = new JTextField();

panel.add(txtStraat);

label = new JLabel("Huisnr:");

panel.add(label);

txtHuisnr = new JTextField();

panel.add(txtHuisnr);

label = new JLabel("Postcode:");

panel.add(label);

txtPostcode = new JTextField();

panel.add(txtPostcode);

label = new JLabel("Gemeente:");

panel.add(label);

txtGemeente = new JTextField();

panel.add(txtGemeente);

label = new JLabel("Land:");

panel.add(label);

txtLand = new JTextField();

panel.add(txtLand);

}

public void mouseReleased(MouseEvent e)

{

boolean gelijk = paswoordenGelijk();

if (gelijk)

{

foutlabel.setText("");

String mail="", tel="", gsm="";

boolean m=false;

Adres a=null;

String paswoord = logingeg.omzettenPaswoord(txtPaswoord.getPassword());

//Persoon p;

String n = txtNaam.getText();

String vn = txtVoornaam.getText();

if (!(e.getSource().equals(hoofdarts)))

{

mail = txtEmail.getText();

tel = txtTelnr.getText();

gsm = txtGsmnr.getText();

m = mails.isSelected();

a = new Adres();

a.setStraat(txtStraat.getText());

a.setHuisnr(txtHuisnr.getText());

a.setPostcode(txtPostcode.getText());

a.setGemeente(txtGemeente.getText());

a.setLand(txtLand.getText());

}

if (e.getSource().equals(patient))

{

Arts ar = (Arts)comboarts.getSelectedItem();

System.out.println(""+ar);

p = new Patient (n, vn, mail, tel, gsm, m, a, ar);

System.out.println(p.toString());

superfacadebean.patientToevoegen((Patient)p, paswoord,logingeg.getLogin(), logingeg.getPaswoord());

}

else

{

if (e.getSource().equals(arts))

{

System.out.println(a.getStraat()+"");

System.out.println(n+""+vn+""+mail+""+tel+""+gsm+""+m+""+a+"");

p = new Arts(n, vn, mail, tel, gsm, m, a);

System.out.println(a+"=adres");

System.out.println(logingeg.getLogin()+"");

System.out.println(logingeg.getPaswoord()+"");

System.out.println(p+"");

System.out.println(paswoord);

System.out.println(p+"");

System.out.println(""+superfacadebean);

superfacadebean.artsToevoegen((Arts)p,paswoord,logingeg.getLogin(), logingeg.getPaswoord());

//panel.remove(arts);

}

else

{

if (e.getSource().equals(therapeut))

{

p = new Therapeut (n, vn, mail, tel, gsm, m, a);

System.out.println(p);

System.out.println(paswoord);

System.out.println("login: " + logingeg.getLogin());

System.out.println("pasw: " + logingeg.getPaswoord());

superfacadebean.therapeutToevoegen((Therapeut)p, paswoord, logingeg.getLogin(), logingeg.getPaswoord());

//panel.remove(therapeut);

}

}

}

setUnabled();

setPaswoordenUnabled();

setKnoppenUnabled();

}

else

foutlabel.setText("Paswoorden zijn niet gelijk");

}

public void setKnoppenUnabled()

{

if (persoon.equals("patient"))

patient.setEnabled(false);

else

{

if (persoon.equals("arts"))

arts.setEnabled(false);

else

therapeut.setEnabled(false);

}

}

public boolean paswoordenGelijk()

{

String pasw1 = logingeg.omzettenPaswoord(txtPaswoord.getPassword());

String pasw2 = logingeg.omzettenPaswoord(txtPaswoord2.getPassword());

if (pasw1.equals(pasw2))

return true;

else

return false;

}

public void mouseClicked(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void setSelected (Persoon p)

{

this.p=p;

System.out.println("set" + p.toString());

}

public void setVelden()

{

if (p != null)

{

setNaam(p.getNaam());

setVoornaam(p.getVoornaam());

setEmail(p.getEmail());

setTelnr(p.getTelnr());

setGsmnr(p.getGsmnr());

//-ADRES GEGEVENS

setStraat(((Adres)(p.getAdres())).getStraat());

setGemeente(((Adres)(p.getAdres())).getGemeente());

setPostcode(((Adres)(p.getAdres())).getPostcode());

setHuisnr(((Adres)(p.getAdres())).getHuisnr());

setLand(((Adres)(p.getAdres())).getLand());

//--STOP ADRES GEGEVENS

setMailOntvangen(p.isEmailOntvangen());

//-ARTS GEGEVENS--

if (persoon.equals("patient"))

setComboarts(((Patient)p).getArts());

else

{//--STOP ARTS GEGEVENS-

if (p instanceof Therapeut)

{

//List<Patient> pat = ((Therapeut)p).getPatienten();

List<Patient> pat = superfacadebean.geefPatienten(logingeg.getLogin(), logingeg.getPaswoord());

if (pat != null)

therapat.setListData((Vector)pat);

}

}

}

}

public void setPaswoordenUnabled()

{

txtPaswoord.setEnabled(false);

txtPaswoord2.setEnabled(false);

}

public void setUnabled()

{

txtNaam.setEnabled(false);

txtVoornaam.setEnabled(false);

if (!(persoon.equals("hoofdarts")))

{

txtEmail.setEnabled(false);

txtTelnr.setEnabled(false);

txtGsmnr.setEnabled(false);

txtStraat.setEnabled(false);

txtGemeente.setEnabled(false);

txtPostcode.setEnabled(false);

txtHuisnr.setEnabled(false);

txtLand.setEnabled(false);

mails.setEnabled(false);

if (persoon.equals("patient"))

comboarts.setEnabled(false);

}

}

public void maakLeeg()

{

txtNaam.setText("");

txtVoornaam.setText("");

if (!(persoon.equals("hoofdarts")))

{

txtEmail.setText("");

txtTelnr.setText("");

txtGsmnr.setText("");

txtStraat.setText("");

txtGemeente.setText("");

txtPostcode.setText("");

txtHuisnr.setText("");

txtLand.setText("");

mails.setSelected(false);

if (persoon.equals("patient"))

comboarts.setSelectedIndex(0);

}

txtPaswoord.setText("");

txtPaswoord2.setText("");

}

public void setNaam(String n) {

txtNaam.setText(n);

}

public void setVoornaam(String n) {

txtVoornaam.setText(n);

}

public void setEmail (String n){

txtEmail.setText(n);

}

public void setTelnr (String n){

txtTelnr.setText(n);

}

public void setGsmnr (String n){

txtGsmnr.setText(n);

}

public void setStraat (String n){

txtStraat.setText(n);

}

public void setGemeente (String n){

txtGemeente.setText(n);

}

public void setPostcode (String n){

txtPostcode.setText(n);

}

public void setHuisnr (String n){

txtHuisnr.setText(n);

}

public void setLand (String n){

txtLand.setText(n);

}

public void setMailOntvangen(boolean b){

mails.setSelected(b);

}

public void setComboarts(Persoon a)

{

System.out.println("setarts: " + a);

comboarts.setSelectedItem((Arts)a);

/*int index=0;

System.out.println(comboarts.getItemCount() + "=count");

System.out.println(comboarts.getItemAt(0));

for (int i=0; i<comboarts.getItemCount(); i++)

{

System.out.println(a);

System.out.println(comboarts.getItemAt(i));

if (((Persoon)a).equals((Persoon)comboarts.getItemAt(i))){

index=i;

System.out.println("gelijk " + (Arts)comboarts.getItemAt(i));

}

}

System.out.println("index: "+ index);

comboarts.setSelectedIndex(index);*/

comboarts.repaint();

}

public String getNaam(){

return txtNaam.getText();

}

public String getVoornaam(){

return txtVoornaam.getText();

}

public String getEmail(){

return txtEmail.getText();

}

public String getTelnr(){

return txtTelnr.getText();

}

public String getGsmnr(){

return txtGsmnr.getText();

}

public boolean getMails(){

return mails.isSelected();

}

public String getStraat(){

return txtStraat.getText();

}

public String getHuisnr(){

return txtHuisnr.getText();

}

public String getPostcode(){

return txtPostcode.getText();

}

public String getGemeente(){

return txtGemeente.getText();

}

public String getLand(){

return txtLand.getText();

}

public Arts getArts(){

System.out.println("geselecteerde arts:" +(Arts)comboarts.getSelectedItem());

return (Arts)comboarts.getSelectedItem();

}

}

>

Rockarena666a at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 5

A bit! A bit! Here's a start:

public class Toevoegen implements MouseListener {

public SuperFacadeRemote superfacadebean = Login.getSuperFacade();

public Container container;

public LoginGegevens logingeg;

public JPanel panel;

private JTextField txtNaam, txtVoornaam, txtEmail, txtTelnr, txtGsmnr, txtStraat, txtGemeente, txtPostcode, txtHuisnr, txtLand;

protected JButton therapeut, patient, arts,hoofdarts;

private JCheckBox mails;

private JLabel label;

protected JLabel foutlabel;

private JComboBox comboarts;

protected JPasswordField txtPaswoord,txtPaswoord2;

private JPanel titel;

protected String persoon;

private List listarts;

protected Persoon p;

private JList therapat;

public JInternalFrame iFrame = new JInternalFrame();

public Toevoegen(LoginGegevens logingeg, String persoon)

{

this.logingeg = logingeg;

this.persoon=persoon;

//container = new Container(); not needed

container = iFrame.getContentPane();

container.setLayout(new BorderLayout());

aanmakenKnoppen();

setPanel();

bewerkPersoon();

foutlabel = new JLabel("");

foutlabel.setForeground(Color.RED);

container.add(foutlabel, BorderLayout.SOUTH);

iFrame.setMaximizable(true);

iFrame.setClosable(true);

iFrame.setResizable(true);

iFrame.setLocation(0,0);

iFrame.setSize(400,400);

iFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

iFrame.setVisible(true);

stresslabo.HoofdForm.dskp.add(iFrame);

stresslabo.HoofdForm.dskp.moveToFront(iFrame);

}

}

Actually, one may want to separate the GUI from the underlying data,

but that's an exercise for another day. This is just pointing out that

you don't have to subclass JInternalFrame, just use it.

DrLaszloJamfa at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 6

Nice, very nice! I didn't know that was allowed! Now I was thinking with this new information that it has to be possible to declare a JInternalFrame the way I want it and then re use it whenever I feel the need. The problem is as follows: what if at any given time I want to change the default_close_operation of my frames? Then I would have to change that in 10 different places. So using your new information I could easily define a frame like I want it to be and then re use it anywhere I want.

If you still feel up to it I would like to know some more of separating the GUi from the underlying data. I think you make a really good point there. Would you put the methods used in this Frame somewhere else then or am I misunderstanding you? Thanx for the help, it's greatly appreciated! My first review of this program by my teacher was not good and I want to do something about it desperately.

Rockarena666a at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 7

> separating the GUi from the underlying data.

This is a reoccurring idea in object-oriented design. One solution is to use

the Model-View-Controller Pattern:

http://en.wikipedia.org/wiki/Model-view-controller

Here the model is the underlying data, the view is the frame and its components, and the controller determines what happens when you press a button, for example.

Doing this well is not easy, but it has the benefit of letting you test parts separately, for example. And you may be able to reuse the model, in non-GUI situations.

DrLaszloJamfa at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 8

I'm already working with that a bit. I'm using Enterprise java beans so a lot of my logic is already separated from my view but I will take a look at it and try to expand this idea.

Would you agree on my idea to define a class that I could re use so all my InternalFrame stuff is in one place and I could then instantiate the class in every other class that needs an Internal Frame?

Rockarena666a at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 9
Yes, watch how the code develops, but if you are avoiding duplication of code, that has to be a good thing.
DrLaszloJamfa at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 10
Many thanks for your help. I've awarded you the Duke stars and if you don't mind I will contact you if I face another problem.
Rockarena666a at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...
# 11
Good luck. You may want to check the Swing forum for Swing-specific threads: http://forum.java.sun.com/forum.jspa?forumID=57
DrLaszloJamfa at 2007-7-12 3:40:38 > top of Java-index,Java Essentials,Java Programming...