Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

JLayeredPane in JPanel?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
This tiny ad is suggesting that maybe she should go play in traffic.
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic