Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: New frame should only show GUI once!

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    gerre (July 30th, 2012)

  6. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: New frame should only show GUI once!

    Glad you figured it out.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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

Similar Threads

  1. My bullseye won't show
    By JeremiahWalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2012, 06:33 PM
  2. [SOLVED] Why doesn't my Applet show up in my browser?
    By SendBorg in forum Java Applets
    Replies: 3
    Last Post: January 29th, 2012, 07:30 AM
  3. Replies: 1
    Last Post: January 19th, 2012, 02:44 PM
  4. [SOLVED] .show is called in card layout, but the frame didn't update
    By nggdowt in forum AWT / Java Swing
    Replies: 2
    Last Post: November 9th, 2011, 01:16 AM
  5. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM