wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes CardLayout / JPanel Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "CardLayout / JPanel Question" Watch "CardLayout / JPanel Question" New topic
Author

CardLayout / JPanel Question

Michael Salvini
Greenhorn

Joined: May 25, 2010
Posts: 20
Hello All,

I am fairly new to Java and have been perusing the boards here picking up little things here and there and I think I have really been learning a lot. Anyway, I have been following this thread http://www.coderanch.com/t/519939/GUI/java/do-you-switch-JPanels-shown and as a result, I learned about the CardLayout Manager and did some experimenting and playing with it which made me wonder about a couple of things.

If you had some menu, maybe JMenuBar or JToolBar, in a JFrame and some of the menu items' actions were to change the content of the JFrame, maybe from one form to another or one picture to another, something like that, Would CardLayout Manager be something appropriate to use for this or would you replace the contents of the JFrame with a new JPanel each time the individual actions are called? From reading the API for CardLayout, it would be best to use it with mulitple Panels, all uniquely named and then call a change to that name with the individual actions of the JMenuItems.

Now, that said brings me to my next curiousity. I recently read up on Threads and Concurrency in Swing and the use of the invokeLater method to send everything to the event dispatching thread. Should calls to change the card be done in a similar way? I have found most of the Swing components say Thread safe or not, but CardLayout does not, although it says it is Serializable which would indicate to me (not sure about this one) that the cahnges should be called through invokeLater.

Sorry about all the questions, just trying to straighten out in my head what I learned and read over the last couple of days.

Thanks for your time,
Mike

Learning Java & Studying for the SCJA
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You don't need to call invokeLater from the EDT itself, except a few cases (like requesting focus). Changing the active panel is not one of them, so from the event handler you can safely change the active panel. From any other thread you'll need invokeLater.

Note that in AWT and Swing, nothing should be considered to be thread safe unless explicitly mentioned to be so (like JTextArea.append).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CardLayout / JPanel Question
 
Similar Threads
Taskbar Iconless JFrame?
Swing
netbeans - change displayed gui content
OO Question
How do you switch JPanels to be shown within the same JFrame