Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanels within JPanels that have no layout manager  RSS feed

 
Josh Sandeman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using absolute layout for a number of JPanels contained within an applet. On one "page" of a wizard within the applet (call it the Configuration Panel), I have a JLabel and a JPanel that itself contains a number of other JPanels (Call is the Device Panel). The JLabel displays just fine, but the Device Panel does not.

Mysteriously, this same Device Panel displays perfectly on another page which also uses no layout manager but to which I added a JLayeredPane (call it the Control Panel). Thinking this might matter, I added a JLayeredPane to the Configuration Panel, but alas it did NOT work. At this point, I'm stumped. Any help would be greatly appreciated!

Josh
San Francisco, CA
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you give the DevicePanel a size() and location() (or a setBounds()),
and then add().
 
Josh Sandeman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did. I've been fiddling around a bit more and noticed something critical. This same object instance-- again, I call it the Device Panel-- is to be displayed in three different JPanels. At least, that's the intent. It seems as though it is only displayed by the last JPanel to have added it during the applet's construction. Why is this happening? Is there a way for me to have the same object instance displayed on multiple containers?

Josh
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can only add it to one.

the workaround might be to have a holding panel set as a gridlayout(2,1),
or a borderlayout (where you use eg south and center)

DisplayPanel is at the bottom, the other panel is set as a cardlayout.
The cardlayout holds your other 3 panels, so as each of the 3 panels are 'shown',
the DisplayPanel remains in view
 
Josh Sandeman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion! I'm curious about why you can add a component to only one container.

The workaround I hit on was to have the container add the Display Panel dynamically every time it becomes visible. This feels like a hack but it's easier given the overall architecture of the application.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest answer is to say a component can't have more than one parent. That's not really an answer, it's merely another effect produced by the same cause. The reason is the architecture isn't designed to be able to do that, just look at the L&F, focus subsystem, etc. to understand that it'd be disastrous. I won't go into details why they made the design choices they made because I don't know.

This does not mean you can't display the exact same thing in two places using the exact same data, it simply means you can't do it by using only one instance of a JComponent. What will work for you is depedent on your program's design and requirements. Here's two examples:

1) Seperate the data from the presentation by storing it and producing a JComponent using that data when requested. Each time you have a panel you need to present this that panel requests the components and your class creates new instances using the data. This gets more complicated if the data may change, you'll need to keep track of instances and update them. If the data changing in one panel needs to affect all you'll have to add listeners as well as updating everything when it happens and synchronizing. For example if you have a name field you might store it as a String, then when requested create a new JTextField with that String as it's value.

2) If you had a toolbar you wanted displayed several places you might simply make a method that returns a new JToolBar with several buttons and use one ActionListener for each type of button. When it requests them you add the appropriate action listener so that no matter which instance of button is pressed the ActionListener is called and you perform your desired action.

Anyway, hopefully that will give you a little direction on what you're going to have to do to display this in several areas.
 
Josh Sandeman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. I am indeed using the MVC architecture; this particular component is an odd duck because it actually is a view. It fetches state from a model, but it needs to be displayed in several different functional contexts. I was hoping to reduce redundancy and thus memory footprint and complexity by having a single instance that switches "modes" depending on the context in which it currently is being displayed. I'll keep fiddlin'.

Thanks so much for your attention!

Josh
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!