Results 1 to 4 of 4
  1. #1
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default How to update JFrame.

    Hi all,

    I am creating a program in which I have a JFrame that switches between different panels that are on it. I am using the remove() method of the JFrame to remove the current panel from the frame and then add the new one on. My question is, how can I get the Frame to update without having to hide the window and bring it back into focus? Or is there a different way I should be doing this? Here is some sample code that I have.

    Java Code:
    public static void main(String[] args) {
    		Main main = new Main();
    		
    		JFrame frame = new JFrame("Gradebook Alpha 1.0");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setLayout(new BorderLayout());
    		frame.add(main, BorderLayout.CENTER);
    		frame.pack();
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    	
    		try {
    			Thread.sleep(1500);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		frame.remove(main);
    		System.out.println("removed");
    		
    		frame.add(new JButton("A"));
    	}
    Thank you for your time.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: How to update JFrame.

    What's wrong with having the frame contain two JPanels and setting their visibility as needed?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: How to update JFrame.

    I am using the remove() method of the JFrame to remove the current panel from the frame and then add the new one on.
    See: How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container). It will manage all this for you.

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: How to update JFrame.

    I just figured it would be better to switch them out, since I will end up using more than two JPanels later, but I guess that would work. And thanks for the link to the CardLayout, I will check it out.

Similar Threads

  1. Update Function won't update!
    By Indigrace in forum New To Java
    Replies: 3
    Last Post: 02-05-2013, 01:49 PM
  2. Update Jlist in JFrame
    By dragon_baby in forum AWT / Swing
    Replies: 4
    Last Post: 05-26-2012, 02:07 PM
  3. How do I Update JFrame Size
    By tyty5949 in forum AWT / Swing
    Replies: 1
    Last Post: 01-24-2012, 02:19 AM
  4. How do I update a JFrame?
    By soulmed in forum New To Java
    Replies: 4
    Last Post: 04-20-2011, 08:45 PM
  5. Update the JFrame after change the Content Pane
    By alisonchan30 in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2010, 06:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •