File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Displaying a JComponent in multiple places Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Displaying a JComponent in multiple places" Watch "Displaying a JComponent in multiple places" New topic
Author

Displaying a JComponent in multiple places

Daniel B. Davis
Greenhorn

Joined: Apr 18, 2003
Posts: 5
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Daniel B. Davis
Greenhorn

Joined: Apr 18, 2003
Posts: 5
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying a JComponent in multiple places
 
Similar Threads
Final Variables & Inner Classes
Javadoc examples
IBM Exam 157 - Questions and Answers (Sample Exam)
CompareTo() method
Creating multiple children threads