Component.add ignores addition of same object ???

S

Superboer

Hello All,

sorry for my ignorance, however why does this:
==================================

import javax.swing.*;


class x{

public static void main (String[] args) {
JPanel j = new JPanel(false);

JTextField jtf = new JTextField("nr");
for (int i = 0 ; i < 10; i++){
j.add(jtf);
}

System.out.println("have added:"+j.getComponentCount()+": to
JPanel...");


}

}
==================================

yield one. ???
And yes i do want to add X times the same object, not X times a new
object....
I can not find a clue in the manual.

Thanks

Superboer.


way fast=http://www.clipjes.nl/clip/nederlands/n/normaal_-
_oerend_hard.html
 
S

Superboer

Hello Sabine,

Thanks for the responce...
But if you want X JTextFields, then you need to create X new
JTextFields. Otherwise, are you trying to display several input fields,
that all refer to the same object? What do you hope to gain from this?

yeah i know but i do sometimes need to display an object twice or
more.....
and this has to be a JTextField or a derived form.
i was kinda baffled that when i add a component, java kicked it out
without telling..
and since i can not find a thing in the manual....

The real question is:

is this allowed or not despite the fact what one wants with it... if
it is
not allowed i need to copy stuff. (not easy and nice....)


Superboer.

way fast=http://www.clipjes.nl/clip/nederlands/n/normaal_-
_oerend_hard.html

Superboer said:
Hello All,
sorry for my ignorance, however why does this:
==================================
import javax.swing.*;
public static void main (String[] args) {
JPanel j = new JPanel(false);
JTextField jtf = new JTextField("nr");
for (int i = 0 ; i < 10; i++){
j.add(jtf);
}
System.out.println("have added:"+j.getComponentCount()+": to
JPanel...");


yield one. ???
And yes i do want to add X times the same object, not X times a new
object....
I can not find a clue in the manual.

Superboer.

But if you want X JTextFields, then you need to create X new
JTextFields. Otherwise, are you trying to display several input fields,
that all refer to the same object? What do you hope to gain from this?

You can re-use the local Variable for adding (if you don't need any
reference to your JTextfields later on), but the new statement needs to
be inside the loop.
 
J

Jan Thomä

i was kinda baffled that when i add a component, java kicked it out
without telling..
and since i can not find a thing in the manual....

The real question is:

is this allowed or not despite the fact what one wants with it... if
it is
not allowed i need to copy stuff. (not easy and nice....)


From java.awt.Container documentation:

* Note that if the component already exists
* in this container or a child of this container,
* it is removed from that container before
* being added to this container.

So to answer your question, it is not possible to add the same component
more than once to a container.

Best regards,
Jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top