Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2005

Tip: Looking for answers? Try searching our database.

Display multiple JPanels in one JFrame using Swing

Thread view: 
rsalmon@tulane.edu - 21 Mar 2005 20:57 GMT
Hi List,

I am hoping to get some advice on how to do this most efficiently.

I have a swing program wich has one JFrame "mainframe".  I also have
several JPanels each with JButtons, text and images on them.

When the program starts I put a blank JPanel on the JFrame like this

mainframe.setContentPane(blankpanel);

As the program starts to run I need to switch out panels and display
other panels and animate some buttons,etc...

So this is the sequence I use for exchanging the JPanels and
redisplaying them.

   mainframe.setContentPane(instpanel);
   mainframe.show();
   mainframe.repaint();

The Problem I have is that this is somewhat not reliable.  Sometimes
the new JPanel does not get updated untill the user inteacts with the
new JPanel or the window gets resized.

Any advice on how to do this most efficiently or better will be greatly
appreciated.

Thank you
Rene
Lloyd - 22 Mar 2005 00:36 GMT
> As the program starts to run I need to switch out panels and display
> other panels and animate some buttons,etc...
[quoted text clipped - 5 lines]
>     mainframe.show();
>     mainframe.repaint();

Have a look at CardLayout for your panel.
John McGrath - 22 Mar 2005 01:28 GMT
> So this is the sequence I use for exchanging the JPanels and
> redisplaying them.
[quoted text clipped - 6 lines]
> the new JPanel does not get updated untill the user inteacts with the
> new JPanel or the window gets resized.

The problem is that the panels are not being validated, although I am not
sure why that is the case.  My guess would be that the panels are not
being invalidated, so the show() call does not validate them.

I am not sure why the code is doing a show() on the frame.  If the frame
is already showing, there is not much point to this, and it could have
some side effects that you do not want, such as bringing the frame in
front of other windows.  Also, setVisible(true) should be used instead of
show(), since show() is deprecated as of Java 5.

Instead of switching out the panels, you should consider placing them in a
panel controlled by a CardLayout.  Then, you just need to tell the layout
to switch the showing panel, and everything should work correctly.  There
is one implication to this approach: the panels must be constructed ahead
of time.  But that should not be a major consideration, unless you have a
large number of panels you want to show in the frame.

Signature

Regards,

John McGrath



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.