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 3 of 3

Thread: Changing JPanels

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Changing JPanels

    public static void loadPanel(JPanel oldPanel, JPanel newPanel){
    		if(oldPanel == null){
    			frame.add(newPanel);
    		}else{
    			frame.remove(oldPanel);
    			frame.add(newPanel);
    			frame.invalidate();
    			frame.revalidate();
    			frame.repaint();
    			frame.setVisible(false);
    			frame.setVisible(true);
    		}
    	}

    I'm using this code to try and switch the frame's panel. It works, but when I move the frame a echo of the old panel still exists. I was thinking and I thought that it may be more efficient to just use the same jpanel over and over again throughout the program. In order to easily point out errors within sections of the program, I kept each JPanel separated in its own class, which are all extended to the frame class. So I think I will try to use one JPanel and transition to sections by removing all components, add the new stuff, and refresh everything. Hopefully this idea will work tomorrow. If someone has found a better way or fix, please post.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,442
    My Mood
    Mellow
    Thanks
    86
    Thanked 476 Times in 467 Posts

    Default Re: Changing JPanels

    You should try CardLayout.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Changing JPanels

    You may need to call pack() on the frame after adding your new content and before showing.
    Read the documentation for invalidate, revalidate, and validate to see what you are calling

Similar Threads

  1. CardLayout which between JPanels
    By ICEPower in forum AWT / Java Swing
    Replies: 3
    Last Post: April 5th, 2013, 03:28 PM
  2. 1 JFrame, more JPanels
    By jknwhz in forum AWT / Java Swing
    Replies: 3
    Last Post: October 24th, 2011, 11:18 PM
  3. Resizing JPanels in JScrollPanes
    By aussiemcgr in forum AWT / Java Swing
    Replies: 4
    Last Post: November 29th, 2010, 01:13 PM
  4. swing - switching JPanels
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 5th, 2010, 09:18 AM
  5. repainting a jframe containing two jpanels
    By musasabi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 11th, 2010, 10:31 PM