thaknk you for replys... actually before posting i tried using
getComponents() and displayed the components, it is diplaying all the
attributes and extra stuff of each component. As Carl and Rhino said to
use getName() method, will i get name of the object ( i mean name of
the component like btn_hello .. JButton btn_hello = new JButton() ) or
should i set name for each component like
btn_hello.setName("Hello_btn") and then access the components through
their names that i set. Can you clear my confusion please;
P.S: hi Rhino h r u..good to c u reply again
Roedy Green - 29 Mar 2006 04:35 GMT
>should i set name for each component like
>btn_hello.setName("Hello_btn") and then access the components through
>their names that i set. Can you clear my confusion please;
the names are arbitrary. I used them in one implementation for
automatically generated labels. You can stuff anything you want in
there.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Monique Y. Mudama - 29 Mar 2006 04:57 GMT
> thaknk you for replys... actually before posting i tried using
> getComponents() and displayed the components, it is diplaying all
[quoted text clipped - 4 lines]
> btn_hello.setName("Hello_btn") and then access the components
> through their names that i set. Can you clear my confusion please;
Nandu, could you please explain what you are trying to accomplish by
using this approach? I have a gut feeling that, whatever you are
trying to do, it could probably be done in a different (and perhaps
more elegant) way.

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Nandu - 29 Mar 2006 05:29 GMT
Hi Mudama
Sorry for the confusion i created. Actually i created 3 JPanels ..
panel1,panel2, panel3 ( where each JPanel has something different to
represent). Now i am placing any one or two or all the three panel on
another JPanel called 'MainPanel' depending on some conditons of the
program. So what i was thinking is that, if i were able to know
currently what are the components on the MainPanel, then i can remove
them using remove(Component name) method and place (add) the required
JPanels on the MainPanel again. Hope you understand my problem
Rhino - 29 Mar 2006 06:07 GMT
> thaknk you for replys... actually before posting i tried using
> getComponents() and displayed the components, it is diplaying all the
[quoted text clipped - 4 lines]
> btn_hello.setName("Hello_btn") and then access the components through
> their names that i set. Can you clear my confusion please;
I haven't used getName() and setName() in a while but I'm pretty sure that
getName() will return only the value that you set with setName(). If you
haven't done a setName on the component, you will get either blank or null,
I don't remember which.
Therefore, if you want to identify your components, simply set a name for
each one as you create it with setName(); then, when you loop through the
result from getComponents(), do a getName() on the component and it will
show you the value that you set with setName().
This examples shows the creation and naming of three panels:
JPanel mainPanel = new JPanel();
mainPanel.setName("Main panel");
JPanel preferencesPanel = new JPanel();
preferencesPanel.setName("Preferences panel");
JPanel otherPanel = new JPanel();
preferencesPanel.setName("Other panel");
Let's say you want to put those three panels on some other component, maybe
another JPanel called outerComponent, you would do that as follows (your
code could vary slightly depending on which layout manager your are using):
JPanel outerComponent = new JPanel();
outerComponent.add(mainPanel);
outerComponent.add(preferencesPanel);
outerComponent.add(otherPanel);
Now, you could use getComponents() to loop through the parts of
outerComponent:
Component[] myComponents = outerComponent.getComponents();
for (Component oneComponent : myComponents) {
System.out.println("component name: " + oneComponent.getName());
}
The output should be (assuming you added the components to outerComponent in
the same order I did):
Main panel
Preferences panel
Other panel
Naturally, once you have verified the name of each component with getName(),
you can do whatever you like with it; 'oneComponent' is now a reference to
the component so you can use it to modify the component.
Please note that this is all coming out of my head: I have not checked the
API or looked at my old code to verify this so it might be slightly wrong.
But I think it should be pretty close.
> P.S: hi Rhino h r u..good to c u reply again
My pleasure :-)
--
Rhino
Thomas Weidenfeller - 29 Mar 2006 14:30 GMT
> thaknk you for replys... actually before posting i tried using
> getComponents() and displayed the components, it is diplaying all the
> attributes and extra stuff of each component. As Carl and Rhino said to
> use getName() method, will i get name of the object ( i mean name of
> the component like btn_hello .. JButton btn_hello = new JButton() )
btn_hello is just a variable name. It is not at all the name of the
object. It just happens to reference an object. It is not uniquely
referencing the object and the object doesn't know which variable holds
references to it.
If your code is structured around the need to evaluate variable names,
then it is badly broken. Think of something else. You probably also want
to study a Java textbook a little bit, and have a look at some OO
principles.
> P.S: hi Rhino h r u..good to c u reply again
Please write real English words. That "style" of communication is
frowned upon here.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Roedy Green - 29 Mar 2006 19:26 GMT
On Wed, 29 Mar 2006 15:30:34 +0200, Thomas Weidenfeller
<nobody@ericsson.invalid> wrote, quoted or indirectly quoted someone
who said :
>> P.S: hi Rhino h r u..good to c u reply again
>
>Please write real English words. That "style" of communication is
>frowned upon here.
I'm told this sort of English evolved by people using cellphones for
text messages where you must type with your thumbs. It is so awkward
people have evolved this telegraph code.
Are people posting to newsgroups with such defective keyboards, or it
is just an ingrained habit?

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.