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: swing - switching JPanels

  1. #1
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default swing - switching JPanels

    hi all,

    About to start working with swing for the first time and have a question. What is the best way to change between different views and/or modes in a program?

    For example, let's say I create a JPanel that I want displayed when the program is first started. It contains 3 buttons that lets the user select between 3 "modes" of the program. I then take this JPanel instance and add it to the JFame's content pane. (All of the other JFrame settings are set and setVisible is set to true).

    When the user starts the program, they see the 3 buttons and are asked to select one. When the user presses a button, I want to get rid of this screen completely and show another JPanel in its place. Then, after the user is done with whatever that option takes them to, they can exit and go back to the main screen (with the 3 buttons).

    Is the following the best way to achieve this?

    Add action listener for the button that does the following:
    1. Remove the "3 button" JPanel from the JFrame's content pane
    2. Add new JPanel to JFrame's content pane

    Alternatively, could you create instances of JDialog and toggle their visibility?

    In short, what is the most efficient way to do this?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,296
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: swing - switching JPanels

    Multiple ways to accomplish this. Take a look at CardLayout, which allows you to readily switch between JPanels. You could also use a JTabbedPane with each tab having the panels/modes/views

  3. The Following User Says Thank You to copeg For This Useful Post:

    bbr201 (August 5th, 2010)

  4. #3
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: swing - switching JPanels

    Ahh, sweet. I didn't know about that CardLayout. It looks like that is exactly what it is designed for. Thanks.

Similar Threads

  1. [SOLVED] Dynamic numbber of JPanels
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 14th, 2010, 02:44 AM
  2. 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
  3. Is it worth switching from PHP to JSP
    By mydarkpassenger in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: April 3rd, 2010, 02:23 AM
  4. Text in Swing
    By whoismrsaxon in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:22 AM
  5. Replies: 1
    Last Post: March 11th, 2009, 04:41 PM