Results 1 to 10 of 10
  1. #1
    amjad905 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    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...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    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 04:36 PM.

  3. #3
    amjad905 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    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");
    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    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.

  5. #5
    amjad905 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    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?

  7. #7
    amjad905 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    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

  8. #8
    amjad905 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    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).

  10. #10
    amjad905 is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Hi,
    Thanks for your help... I managed to iterate through the JPanel and solved my problem... Thanks for helping...

Similar Threads

  1. JSF Calendar Component JS error
    By zazzko in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 02-11-2009, 07:33 PM
  2. COM based component Project
    By jazz2k8 in forum New To Java
    Replies: 0
    Last Post: 07-17-2008, 09:03 AM
  3. How to extend Messages Component
    By Jothi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 05-20-2008, 08:50 AM
  4. Same component on all JTabbedPane
    By java_novice in forum AWT / Swing
    Replies: 4
    Last Post: 08-06-2007, 09:09 AM
  5. Help with custom component
    By Falcon1 in forum AWT / Swing
    Replies: 8
    Last Post: 07-21-2007, 12:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •