Home arrow static arrow Java Programming [Archive] - get Component by name?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - get Component by name?
This topic has 4 replies on 1 page.

Posts:59
Registered: 3/14/03
get Component by name?  
May 23, 2003 5:19 AM



 
Hi,
I have a JFrame with some combos and textfields and a button. When I press the button I have my own implementation of ActionListener that will send information from the JFrame to a servlet. I want to get the values from the combos and textfields, how do I manage this? Is there any way I can access these components by name?

Thanx
 

Posts:4,000
Registered: 24.02.01
Re: get Component by name?  
May 23, 2003 5:26 AM (reply 1 of 4)



 
You need to make sure they are reachable from your actionPerformed method:
class YourFrame implements ActionListener {    private JTextField field;     public YourFrame () {        JButton button = new JButton ("Send!");        button.addActionListener (this);         field = ...    }     public void actionPerformed (ActionEvent event) {        String textInField = field.getText ();    }}
Kind regards,
  Levi
 

Posts:59
Registered: 3/14/03
Re: get Component by name?  
May 23, 2003 5:32 AM (reply 2 of 4)



 
But I don't want the frame to implement the listener cause I have alot of other component to react on actions so I want different ActionListener for different components. So I use :
button.addActionListener(new ButtonListener(this));
where ButtonListener is my own implementation and this is the JFrame.
So I have access to the frame in my ButtonListener but I can't access individual components by name to get their values...
 

Posts:4,000
Registered: 24.02.01
Re: get Component by name?  
May 23, 2003 6:23 AM (reply 3 of 4)



 
You can use an inner class then.
 

Posts:1
Registered: 5/5/04
Re: get Component by name?  
Jun 24, 2004 3:39 PM (reply 4 of 4)



 
So I have access to the frame in my ButtonListener but
I can't access individual components by name to get
their values...

Just give names to the components with
component.setName("String")

and use this code to get the code back:
   /**     * Gets a component by name.    *    * @param comp parent component (may be focused window or some other rootpanecontainer).    * @param name name of the component.    */  private static Component getComponentByName (Component comp, String name)   {      if (comp == null) {         return null;      }       if ((comp.getName() != null) && (comp.getName().equalsIgnoreCase(name))) {         return comp;      }       if (comp instanceof Container) {         Container container = (Container)comp;         int nmembers = container.getComponentCount();          for (int i = 0; i < nmembers; i++) {            Component m = container.getComponent(i);            m = getComponentByName(m, name);             if (m != null) {               return m;            }         }      }       return null;   }


Is this, what you where looking for?
 
This topic has 4 replies on 1 page.