Re: Copying JPanel components to another JPanel



marcussilfver@xxxxxxxxx schrieb:
I want to copy the contents of a jPanel to another JPanel, then I want
to empty the original JPanel.
I unsuccessfully tried the following:

jPanel1 = new JPanel();
jPanel1.setBounds(371, 112, 364, 28);
jPanel1.add(new JLabel("a"));
jPanel1.add(new JLabel("b"));
jPanel1.add(new JLabel("c"));

JPanel jp = new JPanel();

int numComponents = jPanel1.getComponentCount();
System.out.println("Number of components in jPanel1 is " +
numComponents);

for (int i=0; i<numComponents; i++)
{
jp.add(jPanel1.getComponent(i));
Adding a Component to a Container (in this case: to jp) has a side-effect. The Component is first removed from its previous parent, if it is a child of any parent (in this case: of jPanel1). AWT does it that way, to ensure that a child will never belong to more than one parent simultaneously. This behaviour is well-hidden described in the API doc of java.awt.Container#addImpl.
For you this means, that after your "add" operation, the "numComponents" had become wrong, because the actual number of components now has got smaller by 1. Also the numbering of components in jPanel1 has changed.
}
jPanel1.removeAll();

But I get an error like this
java.lang.ArrayIndexOutOfBoundsException: No such child: 2
at java.awt.Container.getComponent(Unknown Source)...

NOTE:
If I replace the row inside the loop with the following row:
System.out.println(jPanel1.getComponent(i).toString());

then I dont get an exception in getComponent, I then get a correct
standard out to the console!

So how can I achieve copying JPanel components? Am I way off?

Try something like this:
int numComponents = jPanel1.getComponentCount();
for (int i = 0; i < numComponents; i++)
{
jp.add(jPanel1.getComponent(0));
//jPanel1.remove(...) isn't needed, because child is already removed
}
or may be like this:
for (int i = jPanel1.getNumComponents() - 1; i >= 0; i--)
{
jp.add(jPanel1.getComponent(i));
//jPanel1.remove(...) isn't needed, because child is already removed
}

--
Thomas
.



Relevant Pages

  • Question on JPanel
    ... If I set a JPanel to not enabled ... Do I have to disable each child one by one? ... I am reposting this question since I did not get a response to a ...
    (comp.lang.java.programmer)
  • Re: Question on JPanel
    ... > If I set a JPanel to not enabled ... > Coming from a VB background, I find this strange. ... > panel, all its children should be automatically disabled. ... Do I have to disable each child one by one? ...
    (comp.lang.java.programmer)
  • mouselisteners of child and parent component both get events?
    ... If I add a component to a component like a JPanel ... can I make the MouseListeners of both the child and parent ...
    (comp.lang.java.gui)
  • finding class parent information
    ... I can get all of the compponets in a class, and get references to ... JLabel called myLabel belongs to parent JPanel called myPanel ... JButton called myButton belongs to parent JPanel called myPanel ...
    (comp.lang.java.programmer)
  • Re: [SWING] Aufruf von JDialog#dispose
    ... Dann hast Du doch schon die Antwort auf Deine Frage. ... Das JPanel verschwindet gemeinsam mit seinem Parent (Container) und damit kann nur der Container entscheiden, ...
    (de.comp.lang.java)