Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-25-2009, 05:30 PM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
amjad905 is on a distinguished road
Default Find Component by name
Hi,
I have created dynamic components and added it to a jpanel and then the jpanel to the jform...
Each component has a name as I set lbl.setname("") etc..
each component has a mouselistener
However, Now I need to find components by name and change the text inside them...
However, I can chnage the text of the selected component but how
do i find a component with another name and change the text of that label with mouselistener...
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-25-2009, 05:33 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,452
Rep Power: 8
Fubarable is on a distinguished road
Default
You could iterate through all the components on a form, and if nested, use recursion. Simpler would be to place the important components into a HashMap<String, JComponent>.

I have a sneaking suspicion that there may be an even better solution available, but you'd have to describe your problem in quite a bit greater detail and probably post some code to see if this is so. Best of luck.

Last edited by Fubarable; 04-25-2009 at 05:36 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-25-2009, 05:50 PM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
amjad905 is on a distinguished road
Default Find Comp
Hi,
How do I iterate through all the components on a form??
Can't I do this?
For (int i=0;i<frame.getComponentCount;i++)
{
if (frame.getComponent().getName().equals("A2"))
{
System.out.println("found element");
}
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-25-2009, 05:52 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,452
Rep Power: 8
Fubarable is on a distinguished road
Default
You would probably have to use recursion. A JFrame's components are first of all in its contentPane, so that's where I'd start. In your recursion search method, if the found component is a container, then see if it holds any components and search those as well.

But to tell the truth, this whole find by name smells a bit funny to me. That's why I'd recommend you tell us your whole problem and post code so we can find a better solution that may have nothing to do with names.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-25-2009, 05:58 PM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
amjad905 is on a distinguished road
Default Find Comp
Basically, I need to find a component with a name and then change the background color of that component...

hmm... how do I search for the components in the JFrame can u provide a code for it??
frame.getContentPanel().wht???

I will try to paste the code by breakin it up as it is alot messy and will very hard to understand
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-25-2009, 06:05 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,452
Rep Power: 8
Fubarable is on a distinguished road
Default
Again, more detail is important: How do you get the components name in the first place? In other words, how do you decide which component (regardless of the name) needs to have its color changed? Is it the one clicked? Random? By some other method?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-25-2009, 06:10 PM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
amjad905 is on a distinguished road
Default Find Comp
Hi,
Ok There is a header whose name is set as A, B, C etc
then there is a labels whose name is A1, A2, B1, B2 etc
Just like Excel....

A1, A2 etc has a mouselistener implemented...
SO If I click on A1 I get the Alphabet and leave the number...
In this case , It becomes A...
So I want to change the label background color whose name is A
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-25-2009, 06:32 PM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
amjad905 is on a distinguished road
Default Find Comp
Can you please tell me how to iterate through a JForm or a JPanel...
The name of the component I get through the MouseListener
public void mouseClicked(MouseEvent e) {
comp = (JComponent) e.getSource();
comp.setBackground(Color.RED);
}

but how do I search for a label with a specific name as the
comp = (JComponent) e.getSource();
comp.getName() gives A1 but I want to find another JLabel with A as I remove the 1 from the getname and try to find the JLabel with A
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-25-2009, 06:48 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,452
Rep Power: 8
Fubarable is on a distinguished road
Default
are all the A1, A2, ... Ax labels on one row? Likewise for the Bx, Cx, etc labels? Could you place the A labels into an ArrayList<JLabel> aLabel collection, and same for the other labels? Then just iterate through the selected collection and change the background color of the entire row (I'm guessing that that is what you want to do).
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-25-2009, 06:55 PM
Member
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0
amjad905 is on a distinguished road
Default
Hi,
Thanks for your help... I managed to iterate through the JPanel and solved my problem... Thanks for helping...
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSF Calendar Component JS error zazzko JavaServer Faces 1 02-11-2009 08:33 PM
COM based component Project jazz2k8 New To Java 0 07-17-2008 10:03 AM
How to extend Messages Component Jothi JavaServer Faces 0 05-20-2008 09:50 AM
Same component on all JTabbedPane java_novice AWT / Swing 4 08-06-2007 10:09 AM
Help with custom component Falcon1 AWT / Swing 8 07-21-2007 01:39 PM


All times are GMT +2. The time now is 10:49 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org