Tabbed pane with several time the same panel
I have three tabs, the first one contain a component, the second one another component, and the third one contains both component from the fi开发者_如何转开发rst and second tab. On my third tab both components don't appear. Do I have to make a clone of my first and second component to have it on the third tab? What if some properties of my components change?
As others have mentioned, a Swing JComponent
cannot be added to more than one other JComponent
(its parent).
If your concern is to share the content of components then you generally have an easy option: since Swing components are based on MVC, they all have a Model which you can share between several JComponent
s.
For instance, JTextComponent
(JTextField
and JTextArea
are JTextComponent
s) has a Document
as its model, this Document
can be shared as in the following snippet:
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
field2.setDocument(field1.getDocument());
Now if you want to synchronize other properties (e.g. number of columns of JTextField
), you'll have to use a PropertyChangeListener
as someone else suggested.
A Swing component can only have one parent at a time. So if you try to add an instance of a component to multiple containers this will not work.
So instead you have to create an instance of the components for every container you would like to add it to.
Multiple tabs cannot have the same component. There is a bug for this in the Sun database:
Bug: 4176095 - Clarification that no 2 Tabs in JTabbedPane can have same component
There is a proposed fix which has not been incorporated though.
If you want to achieve this. You can do the following steps - Add tab listner - Whenever a tab is selected you can add the components that need to be displayed in the selected tab panel and show it. (i.e., whenever tabpanel is getting displayed we are going reset the components parent details)
Yes, any swing component can be added only to one container.
On each tab you should have separate components, using listeners you can manipulate the properties of those components.
To solve problem like this you should get familiar with MVC design pattern,
精彩评论