• Post Reply Bookmark Topic Watch Topic
  • New Topic

sharing a static jpanel

 
andy keidel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a bunch of panels, and they all share a common subpanel. To do the subpanel, I made it a static JPanel. But when I create multiple objects that share this subpanel, only the *most recently created* object shows the subpanel. In the other objects (which are also JPanels), the subpanel becomes invisible. How can I fix this?
To clarify, when I add the static JPanel to a new panel, the previous panel that I added it to no longer sees it. I want to fix this so I can use just one subpanel object referenced by all other panels.
Thanks,
Andy
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JComponent can only have one parent component. ie. multiple panels cannot share the same subpanel and display it at the same time.
Instead just create as many instances of the sub panel as you need.
D.
 
andy keidel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer! So, what's the reason for this limitation? Swing in particular seems to be really big on using "listeners", so why don't they make it possible to have multiple parents listen to the same static JPanel and render it whenever it changes? Offhand I'd think this should be easy in Java; instead it appears impossible.
[ March 02, 2004: Message edited by: andy keidel ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by andy keidel:
So, what's the reason for this limitation?

Why does each foot belong to one and only one person?
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by andy keidel:
Thanks for the answer! So, what's the reason for this limitation? Swing in particular seems to be really big on using "listeners", so why don't they make it possible to have multiple parents listen to the same static JPanel and render it whenever it changes? Offhand I'd think this should be easy in Java; instead it appears impossible.
[ March 02, 2004: Message edited by: andy keidel ]

There doesn't seem to be much point really. But if you wish to create a JPanelModel & JPanelRenderer classes, then go ahead -> there's nothing to stop you.
My guess is there's probably a different approach to your problem that can be solved using the standard widgets. What exactly are you trying to achieve ?
D.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I did something to get similiar results... I created a subclass of JPanel and overrode the clone() method to create a new JPanel subclass, using new components, but re-using the models of the original components.... and recursively doing the same thing on any any JPanel subclass inside this one.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, create a model class that comprises the information your panel needs to display. To create multiple panels :

Advantage, it'll work with bespoke components.
Disadvantage, you have to do a bit more work yourself.
D.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!