Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying a JComponent in multiple places

 
Daniel B. Davis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I display a JComponent, such as a Box in multiple places. Should be simple, I thought; all Java variables are references, so just store it many times. Didn't work. It was created and added to one pane of a JTabbedPane. Then copied by an assignment statement to 2 different panes so that its contents, once created, would be visible on those other pages amidst various different stuff for reference purposes. Only the last place added had it visible. If I commented out the last place, then the next-to-last, and if that commented out, then the first. Weird, and it violates what I thought I knew, -- all Java variables are references.

I tried storing it in an Object, then recasting it back. No luck. Any Ideas?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Components can only have one parent container; each time you add a component to another container you remove it from the previous container. There is just one possibility - create multiple identical instances of the component.

With some components you can share a model (e.g. TreeModel with JTree, TableModel with JTable) or Actions (JMenuItem, JButton, JCheckBox), but that's about as far as you're going to get.
 
Daniel B. Davis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply. Although I knew that, its significance to the issue was not apparent. I have created three separate components, so that each may be reparented upon copy. It works. Thanks again

dbd
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic