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?
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"); } } }
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.
first.jpgsecond.jpg