File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JLayeredPane in JPanel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JLayeredPane in JPanel?" Watch "JLayeredPane in JPanel?" New topic
Author

JLayeredPane in JPanel?

J Chandler
Ranch Hand

Joined: May 17, 2003
Posts: 32
Hey everyone,

The BorderLayout.WEST area of my main JFrame has buttons that trigger certain events.

When an event occurs, the content of the BorderLayout.CENTER area of the main JFrame should change, i.e. one component is substituted for another using add and remove methods.

This works fine with single items, but now I need to display a mini layout (background, JList, and 2 text-based components) within that content area.

So, I created a new class that extended JPanel and contained a constuctor specifying all the requirements of the display, with the idea that I would keep the code for the new layout separate for clarity and just instantiate an instance of it in my actionPerformed(....) event method.

Buttonclick --> actionPerformed --> make instance of JPanel --> add to BorderLayout.CENTER

I intended to use JLayeredPane to arrange the new display within my JPanel, but have found that I need a JFrames' contentPane to enable the layered pane,
// jlp=JLayeredPane
// jf=JFrame
e.g. jlp = jf.getLayeredPane();

I tried it with the JPanel -
e.g. jlp = this.getLayeredPane(); // this=the current JPanel instance/object

but that doesn't work.

How can I make a separate customised JPanel with layered components, create an instance of it in my main class and drop it into a JFrame layout if my JPanel doesn't have a reference to the main JFrame?

I have tried extending JFrame instead of JPanel in the separate class - that works, but it has a title bar and I don't want that to show. I need it to appear like a panel, that's all.

Any comments?

Regards, J
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
Hi J,

I have done a similar UI. I just put a new JPanel with CardLayout
and I added all the "subdialogs" as cards into the cardlayout
beforehead and when an action was trigged I just selected the
right page.

I am not sure that JLayeredPane is what you want.

It is possible to create a new JPanel whenever there is the
actionPerformed, but you will have to do things like pack().
Well I tried to play with it and I wasn't very successful.

Hope it helps.
Petr


Get a better web browser:<br /><a href="http://www.mozilla.org/products/firefox/switch.html" target="_blank" rel="nofollow">http://www.mozilla.org/products/firefox/switch.html</a>
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I agree that a card layout might be an elegant solution. If you do want to swap panels I would suggest to make up the JPanel and add/remove it as desired rather than making a new one each time.

It is easy to paint graphics and/or an image on the background of a JPanel and then add components to it.

If you do a separate class that extends JPanel (as you said) you can instantiate it in your JFrame class and keep a reference to it for the add/remove operations.
J Chandler
Ranch Hand

Joined: May 17, 2003
Posts: 32
Petr,

thank you so much for your comments re. my JPanel query - I have looked at CardLayout and I agree that it would be equally as useful as the 'add and remove' methods.

However, this still leaves me with my original problem....let me explain myself....
the reason I had chosen to implement JLayeredPane is that I wanted a certain look to my finished display. I wanted a backgound image to cover the entire area of the JPanel (added as usual e.g. into BorderLayout.CENTER - the image would expand to fill the JPanel as the background image is the only component added to this layer)

Then, I wanted to utilise JLayeredPane in order to place my other components on top of the background image using absolute positioning - this way, I could achieve a more balanced look rather than have an image in the middle or pushed to one side etc.

I know this approach works - I tried it, but as I mentioned before, that was using the contentPane of a JFrame and I don't want another title bar to appear inside of my existing main JFrame.

Craig,

If you do a separate class that extends JPanel (as you said) you can instantiate it in your JFrame class and keep a reference to it for the add/remove operations.


This is exactly what I'm trying to do - I have several more different JPanels to design and if I just wrote them all in the actionPerformed method it would be WRONG

It is easy to paint graphics and/or an image on the background of a JPanel and then add components to it.


Having read my reasons for choosing JLayeredPane for my design, are there any other ways you know to achieve this effect?

If not, I may have to do a re-think - I learn from all my mistakes (trust me, there have been many)

Thanks again, Petr and Craig!
Regards, J
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Here's another way to put everything in a single component. Render the centered image on the JPanel and add your components to it. If the JPanel is not going into BorderLayout.CENTER you may have to provide some minmal size information with setPreferredSize or by overriding getPreferredSize. You can add graphics to the JPanel this way too.
J Chandler
Ranch Hand

Joined: May 17, 2003
Posts: 32
Craig,

I ran your AnotherPossibility code with my image and it looks like we have a solution!

I can create a class that extends JPanel without compromising my ideal layout. Of course I need to work on the exact positioning using fields of GridBagLayout/GridBagConstraints, but I can handle that.

Thanks for your excellent input, mate.
Regards,
Jules
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JLayeredPane in JPanel?
 
Similar Threads
Perhaps this time the fleet needs to sail
Beginning GUI programming exercise. Please help to figure out why it does not compile.
JTextArea scroll bar
JFrame resizing -- what is normal behavior?
How do I get layered items to show outside the parent frame?