Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Components in JPanel

Thread view: 
Nandu - 28 Mar 2006 23:33 GMT
Hi
  I am placing JLabels, JButtons , JRadioButtons and other components
on the JPanel at random. Now how can i know what are components are
there in the JPanel at a instance of time. How can get their names. For
example if btn_Hello is a JButton and if it is on the JPanel then i
should be able to get btn_Hello is on the JPanel.

thank you very much
Monique Y. Mudama - 29 Mar 2006 00:28 GMT
> Hi I am placing JLabels, JButtons , JRadioButtons and other
> components on the JPanel at random. Now how can i know what are
> components are there in the JPanel at a instance of time. How can
> get their names. For example if btn_Hello is a JButton and if it is
> on the JPanel then i should be able to get btn_Hello is on the
> JPanel.

You can use getComponents(), iterate through the results and check for
instanceof JButton.

I'm not really sure what that buys you, but you can do it.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Carl - 29 Mar 2006 00:32 GMT
Hello Nandu,
My reply is inline...

> Hi
>    I am placing JLabels, JButtons , JRadioButtons and other components
> on the JPanel at random. Now how can i know what are components are
> there in the JPanel at a instance of time.

The Container.getComponents() method will retrive an array of all
Component objects in the Container.

> How can get their names. For
> example if btn_Hello is a JButton and if it is on the JPanel then i
> should be able to get btn_Hello is on the JPanel.

How about the getName() method of Component objects?

> thank you very much

Your welcome,
Carl.
Rhino - 29 Mar 2006 01:07 GMT
> Hello Nandu,
> My reply is inline...
[quoted text clipped - 12 lines]
>
> How about the getName() method of Component objects?

If I remember correctly, getName() returns a blank (or null) if you call it
without first using setName() to give the component a name. So, you will
want to make a point of giving the component a name with setName() when you
create it, e.g. setName("my first button").

--
Rhino
Nandu - 29 Mar 2006 04:24 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() ) 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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.