Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #2242
| From | "bcr666" <bcr666@THRWHITE.remove-dii-this> |
|---|---|
| Subject | Re: Copying JPanel compon |
| Message-ID | <1187181585.889927.207870@j4g2000prf.googlegroups.com> (permalink) |
| Newsgroups | comp.lang.java.gui |
| References | <newscache$k7vrmj$0sj$1@news.ops.de> |
| Date | 2011-04-27 15:37 +0000 |
| Organization | TDS.net |
To: comp.lang.java.gui
On Aug 14, 11:31 am, Thomas Fritsch <i.dont.like.s...@invalid.com>
wrote:
> marcussilf...@gmail.com 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);
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Copying JPanel components "marcussilfver" <marcussilfver@THRWHITE.remove-dii-this> - 2011-04-27 15:37 +0000
Re: Copying JPanel compon "Thomas Fritsch" <thomas.fritsch@THRWHITE.remove-dii-this> - 2011-04-27 15:37 +0000
Re: Copying JPanel compon "bcr666" <bcr666@THRWHITE.remove-dii-this> - 2011-04-27 15:37 +0000
Re: Copying JPanel compon "Ville Oikarinen" <ville.oikarinen@THRWHITE.remove-dii-this> - 2011-04-27 15:37 +0000
Re: Copying JPanel compon "Larry Barowski" <larry.barowski@THRWHITE.remove-dii-this> - 2011-04-27 15:38 +0000
csiph-web