How to program toggle-able JPanels?
April 10, 2006 12:32 PM
Subscribe
Java question: how to program a toggle-view in a GUI?
Basically, I want to have two seperate JPanels that are the same size and occupy the same space in the frame, but only allow one to be visible at any time, so that the view can be toggled in between these two panels with a radio button. Using the glass pane seems to be out because it covers the menu bar, and I've had absolutely zero luck with getting the layered pane to work out, so I'm kind of stuck. Any help would be appreciated!
posted by invitapriore to computers & internet (8 comments total)
1. Make a JPanel to hold the 2 subpanels and set its size.
2. Create the 2 sub JPanels and set their preferred sizes.
3. Write a custom LayoutManager to position the 2 sub panels on the parent panel at the correct location.
4. Call panel.setVisible(false) on the first sub panel and panel.setVisible(true) on the other
5. Code the event handler for your radio buttons to set the visibilty on the sub panels correctly
You shouldn't need any class panes or anything- just the setVisible() function and tha ability to do your own layout.
posted by gus at 12:55 PM on April 10, 2006