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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "CardLayout / JPanel Question" Watch "CardLayout / JPanel Question" New topic

CardLayout / JPanel Question

Michael Salvini

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 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,

Learning Java & Studying for the SCJA
Rob Spoor

Joined: Oct 27, 2005
Posts: 20275

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).

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: CardLayout / JPanel Question
It's not a secret anymore!