Copying JPanel components to another JPanel - Java

This is a discussion on Copying JPanel components to another JPanel - Java ; 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 ...

+ Reply to Thread
Results 1 to 5 of 5

Copying JPanel components to another JPanel

  1. Default Copying JPanel components to another JPanel

    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));
    }
    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?


  2. Default Re: Copying JPanel components to another JPanel

    marcussilfver 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

  3. Default Re: Copying JPanel components to another JPanel

    On Aug 14, 11:31 am, Thomas Fritsch <i.dont.like.s...@invalid.com>
    wrote:
    > marcussilf... 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));

    >
    >
    > 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


    For loops that change the quantity of things, I like to use a while
    loop eg.

    while (jPanel1.getComponentCount() > 0) {
    jp.add(jPanel1.getComponent(0);
    }

    or I use this a lot for removing rows from a JTable

    DefaultTableModel model = (DefaultTableModel)tblMyTable.getModel();
    while (model.getRowCount() > 0) {
    model.removeRow(0);
    }


  4. Default Re: Copying JPanel components to another JPanel

    On Tue, 14 Aug 2007 marcussilfver wrote:

    > I want to copy the contents of a jPanel to another JPanel, then I want
    > to empty the original JPanel.


    To me this whole business of moving components sounds questionable. What
    are you *really* doing?

    - Ville Oikarinen

  5. Default Re: Copying JPanel components to another JPanel


    "Ville Oikarinen" <ville@oikarinen.org> wrote in message
    news:Pine.LNX.4.56L0.0708151532310.27727@localhost...
    > On Tue, 14 Aug 2007 marcussilfver wrote:
    >
    >> I want to copy the contents of a jPanel to another JPanel, then I want
    >> to empty the original JPanel.

    >
    > To me this whole business of moving components sounds questionable. What
    > are you *really* doing?


    Certainly there are good reasons for doing so, but if you want to
    move a group of components, usually you would put them all in
    one panel and just move the whole panel. This may require adding
    a panel to the component hierarchy that has no other purpose than
    to be the moveable container.



+ Reply to Thread

Similar Threads

  1. JPanel in JList
    By Application Development in forum Java
    Replies: 0
    Last Post: 06-09-2007, 08:59 AM
  2. Jpanel and Canvas
    By Application Development in forum Java
    Replies: 4
    Last Post: 03-15-2007, 07:33 PM
  3. How heavy is a JPanel?
    By Application Development in forum Java
    Replies: 3
    Last Post: 11-15-2006, 01:44 PM
  4. Help with a JPanel
    By Application Development in forum Java
    Replies: 4
    Last Post: 08-25-2006, 04:54 AM
  5. JPanel separators
    By Application Development in forum Java
    Replies: 2
    Last Post: 07-25-2006, 10:01 PM