Forums Register Login

JLayeredPane in JPanel?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6460 times.
Similar Threads
Beginning GUI programming exercise. Please help to figure out why it does not compile.
How do I get layered items to show outside the parent frame?
JFrame resizing -- what is normal behavior?
Perhaps this time the fleet needs to sail
JTextArea scroll bar
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:53:40.