2 Attachment(s)
New frame should only show GUI once!
Hi guys
First, the code works as it was supposed to, but I have a little problem with it still. I am opening a new frame from an actionListener with gui contents. The first time I open the new frame, there's nothing wrong. But if I for some reason need to close that frame, and open it again (I'm using it to add data to a database, so that's a quite likely scenario), the gui contents is shown twice. If I close and open it again, the gui content is shown three times and so on.
So my question is, how do i "erase" the programs memory so that I continously can open that frame with the gui content only shown once?
Code :
public class createKunde implements ActionListener{
public void actionPerformed(ActionEvent a) {
String input = kunde.kundeText.getText();
String e = "e";
String p = "p";
if (input.equals(p)) {
new privatKunde();
}
else if(input.equals(e)) {
new erhversKunde();
}
else {
JOptionPane.showMessageDialog(null, "forkert indtastet");
}
}
}
Code :
public class privatKunde {
public static JFrame privatFrame = new JFrame();
private JTextField cprText, fnavnText, enavnText, adresseText, byText, tlfText, emailText, afdelingText;
private JLabel cprLabel, fnavnLabel, enavnLabel, adresseLabel, byLabel, tlfLabel, emailLabel, afdelingLabel;
public static JButton privatButton = new JButton("Indtast kunde");
public privatKunde() {
privatFrame.setSize(300, 625);
privatFrame.setTitle("Privatkunde");
privatFrame.setLayout(new GridLayout(2, 0));
//Panel laves og layout tilføjes
JPanel Panel = new JPanel();
Panel.setLayout(new BoxLayout(Panel, BoxLayout.Y_AXIS));
//Labels og textfields tilføjes til panelet
cprLabel = new JLabel("CPR:"); Panel.add(cprLabel);
cprText = new JTextField(); cprText.setColumns(5); Panel.add(cprText);
fnavnLabel = new JLabel("Fornavn:"); Panel.add(fnavnLabel);
fnavnText = new JTextField(); Panel.add(fnavnText); fnavnText.setColumns(5);
enavnLabel = new JLabel("Efternavn:"); Panel.add(enavnLabel);
enavnText = new JTextField(); Panel.add(enavnText); enavnText.setColumns(5);
adresseLabel = new JLabel("Adresse:"); Panel.add(adresseLabel);
adresseText = new JTextField(); Panel.add(adresseText); adresseText.setColumns(5);
byLabel = new JLabel("By:"); Panel.add(byLabel);
byText = new JTextField(); byText.setColumns(5); Panel.add(byText);
tlfLabel = new JLabel("Telefonnr:"); Panel.add(tlfLabel);
tlfText = new JTextField(); tlfText.setColumns(5); Panel.add(tlfText);
emailLabel = new JLabel("Email:"); Panel.add(emailLabel);
emailText = new JTextField(); emailText.setColumns(5); Panel.add(emailText);
afdelingLabel = new JLabel("Afdeling:"); Panel.add(afdelingLabel);
afdelingText = new JTextField(); afdelingText.setColumns(5); Panel.add(afdelingText);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(privatButton);
privatFrame.add(Panel);
privatFrame.add(buttonPanel);
privatFrame.setVisible(true);
privatButton.addActionListener(new ændreAnsat());
}
}
I have attached a couple of pictures to show the problem.
Attachment 1359Attachment 1360
Re: New frame should only show GUI once!
That kind of error can happen if you keep adding listeners. First there is one, then two, then three, etc.
Each time an event happens all the listeners are called.
Re: New frame should only show GUI once!
I guess that's logic. But what is the correct way to use listeners then? I kinda need a listener to every button. The way I use it in this program is shown in the last line in the second class. And that class(ændreAnsat) then implements ActionListener. How should that be done to avoid these problems?
Re: New frame should only show GUI once!
Only add a listener one time. Make sure you don't add the same listener to the same component more than one time.
Re: New frame should only show GUI once!
Well it turned out that it's not the problem. But you put me on the track so still thanks. The problem is there are components in the different classes that have been called the same name and have been public.
Re: New frame should only show GUI once!
Re: New frame should only show GUI once!
maybe you don't want to close it and create a new one if you know you will often resume it. you could hide it and keep the state alive. i mean - depending on your overall purpose