Results 1 to 4 of 4
  1. #1
    uiproject is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Question Find a component by its name

    I have created a Swing UI and the UI contains a frame, a panel and may be more components like buttons and text fields. I have speech recognition enabled for this UI. So, the user can say "set the background of the frame, a, to black". Now my problem is when the user says the above line I need to detect what component the user is talking about. I know the component name is 'a' but how do I get that component? Can someone tell me how to do this?

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

    Default

    If you have components that you want to have a handle on, you'll need to get a reference to them. Probably the easiest way is to create and use a HashMap<String, Component>.

    By the way, what do you mean by component name? If you mean variable name, then forget it, since that information is essentially erased when the program is running. You likely want to associate each changeable component with a String, and a HashMap is an easy way to do this.

  3. #3
    uiproject is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Re:

    Thanks for the suggestion. Hashmap is a great idea. However, I just found another way like this:


    JFr is my frame. tokens[5].getSpokenText() is the component name that the user wants to set a background for. parentComponent is the component which we are looking for!

    JComponent parentComponent = null;
    for (int i=0;i<jFr.getContentPane().getComponentCount();i++ )
    {
    if (jFr.getContentPane().getComponent(i).getName().eq uals(tokens[5].getSpokenText()))
    {
    parentComponent = (JComponent) jFr.getContentPane().getComponent(i);
    break;
    }
    }

    Thank you for your very quick response :)

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

    Default

    That won't work if you have components nested in each other, as will happen with any GUI that's more than the most simple. If you're going to use the Component.name property, this could work if you create a recursive method though.

Similar Threads

  1. Component not updating??!!
    By XmisterIS in forum New To Java
    Replies: 5
    Last Post: 09-03-2010, 11:25 AM
  2. Component to use ?
    By pbaudru in forum AWT / Swing
    Replies: 2
    Last Post: 02-17-2010, 09:55 AM
  3. component centering
    By BigBear in forum AWT / Swing
    Replies: 3
    Last Post: 01-24-2010, 11:48 PM
  4. add component to component
    By Omarero in forum New To Java
    Replies: 7
    Last Post: 05-17-2009, 02:39 PM
  5. Find Component by name
    By amjad905 in forum Advanced Java
    Replies: 9
    Last Post: 04-25-2009, 05:55 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
  •