aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Layers of JPanels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Layers of JPanels" Watch "Layers of JPanels" New topic
Author

Layers of JPanels

Lance Gray
Greenhorn

Joined: Dec 06, 2011
Posts: 20
I have this idea that I am planning to implement in this project of mine once I finish the actual content and is working on design.

Basically, what I want to do is to have only one JFrame with nothing in it but a JPanel (let's call this mainFrame and mainPanel, respectively). Then I have different JPanels that represents each 'screen' of the project (like titleScreen, resultScreen, reportScreen, etc). Also, I have yet more panels which is the 'dialog boxes' (like dialogConfirmSave, dialogConfirmExit, etc). These dialog boxes will have designs in it (it is required for my project to be visually appealing since it's for kids) and is smaller than the normal 'screen panels'

What I'm thinking is something like this:
1. Load mainFrame.
2. Put titleScreen inside mainPanel.
3. After a method call from a button in titleScreen, titleScreen will be disposed and a new panel will take its place. This new panel is still inside mainPanel. (See Figure 1)

4. If, for example, the user clicks a button (say, buttonExitToWindows) that brings up a dialog box (dialogConfirmExit) on top of the panel inside mainPanel. (See Figure 2)

5. When dialogConfirmExit is shown, all other panels below it (titlePanel, in this case) are simply disabled and no access is allowed. All threads started by that panel are also 'paused' for that period. When dialogConfirmExit is disposed, all access on the panel below it are returned and all threads that was 'paused' will resume.

Is this possible? If so, how can I accomplish this?



[Thumbnail for state1.png]


[Thumbnail for state2.png]

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
your description sounds ideally suited to a CardLayout (multiple panels loaded, only one of them 'showing' at a time),
all controlled by e.g. a JMenu
Lance Gray
Greenhorn

Joined: Dec 06, 2011
Posts: 20
Michael Dunn wrote:your description sounds ideally suited to a CardLayout (multiple panels loaded, only one of them 'showing' at a time),
all controlled by e.g. a JMenu

Is CardLayout still suitable even if one panel requires a number of parameters before being shown and these parameters determine the contents of the panel?

Because as far as I know, when using CardLayout, you make multiple panels (the 'cards') and then showing them one at a time. For example we have panels A and B. If I am on A and I want to show B, I will just tell the panel to show B (not create another instance of the panel B). Am I correct? If so, what if B requires an argument (say an integer named myInteger) and this argument controls what the panel does (changes the text, determines which file to open, etc) For example:

I am on A. Then I call B(1). I am now on B with myInteger being 1. I returned back to A. I called B again, but this time with 2 as the argument. Will I have to create another instance of B instead of using the preloaded panel B?

I'm sorry if I'm not making any sense or if I'm confusing you. I'll try to post my work if needed.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Is CardLayout still suitable even if one panel requires a number of parameters before being shown and these parameters determine the contents of the panel?

a lot would depend on the type of changes to be made
e.g. if it's just the text of a JLabel, or the contents of a file to be read into a JTextArea,
change the details inside actionPerformed just before the call to 'show'.
if the components/positions change, that gets a bit messier, so perhaps another setup would be better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Layers of JPanels