What is java.awt.Component.getName() and setName() used for?
Asked Answered
C

9

23

What is java.awt.Component.getName() used for? It always seems to be null in the applications I build with NetBeans. I'm thinking of storing some help text per component in it -- I don't want to use the tooltip, I have another panel where I'll show the help text.

Creepy answered 22/10, 2008 at 19:6 Comment(0)
A
21

Component.setName(..) is used in the JDK mostly by the look and feel implementation classes to set ID-like strings for each component, e.g. BasicOptionPaneUI might call it on a button component to set its name to "OptionPane.button".

The getName() is used in toString() methods, when setting the names of child components inside a Composite/parent Component and in AWT and Swing debug logging code. I suspect strongly that the getName() method is also used by some AWT/Swing testing frameworks.

So if you're not dependent on any of the above uses of getName(), you might try using it for your help messages, though I would not recommend it.

Maybe you should reconsider your design? Use the name to do some lookup in a hashmap that loads the help text from a resource bundle?

Alanalana answered 22/10, 2008 at 19:55 Comment(0)
P
7

I haven't seen it used for anything by the framework. Its useful if you have components being passed in to a method so you can ask their name to decide how to handle them. Also, many UI testing frameworks use this to allow you to refer to the components by name in the testing scripts. I don't see any reason you can't use it for help text though.

Perrone answered 22/10, 2008 at 19:11 Comment(2)
We use it for robot/UI testing.Tipcat
We also use it for robot/UI testing.Yuletide
C
2

Herman Lintvelt's answer ended up being the correct one for my app.

I created a resource bundle named HelpText.properties. It contains name=value pairs. I setName()d each of my Components with the "name" from the name=value pair. I then used a the frame's getGlassPane() to capture all mouse movements. When a mouse runs over a named component, it looks up the name in the bundle, displays help if available and forwards the mouse motion to along to the actual Component.

Whew. Only 2 days worth of dinking around. I'm finally starting to get used to Java :)

Creepy answered 22/10, 2008 at 21:55 Comment(1)
Glad I could help. Enjoy Java.Alanalana
S
2

FEST uses the name of a Component to identify it in testcases.

Sherri answered 4/9, 2012 at 14:28 Comment(0)
L
1

The component.getName() method is mostly used with listeners. If you set the name of a component (component.setName(name)) you can call to that specific component from within a method of a Listener.

Example:

public void someMethodOfsomeListener(SomeEvent e){
   if (e.getComponent().getName().equals(component.getName())
      //do stuff...
}

Be aware that you have to explicitly set the name of the component, otherwise it will return null.

Longeron answered 17/7, 2009 at 15:8 Comment(2)
If you have the component reference in the listener you could use e.getComponent() == component too which is less error-prone. I would consider using multiple listeners too.Amaranthine
Components can have the same name, too. Do not confuse the above test for component equality.Yuletide
W
0

Also, since I think java.awt.Component is a heavyweight object in X, programs like xwininfo and xwd might allow you to specify it by name.

I just tried it with a JFrame, and setName didn't set the name of the window, the window was named by the string I passed in the constructor. But I don't have any awt-only example code to test with, so I could be wrong about what I wrote above.

Welcome answered 22/10, 2008 at 19:26 Comment(0)
M
0

I use it for handling listeners in one single class apart. I receive as a parameter the component which contains my object.addListener not as a container but as the class that contains that object. Thanks Vivavinyl for the the tip of setting the name first. It was useful and worked.

Mozzetta answered 26/7, 2009 at 22:24 Comment(0)
D
0

This is what I use getName() for:

    Frame[] frames = JFrame.getFrames();

    for (int i = 0; i < frames.length; ++i) {

        //get the frame
        Frame frame = frames[i];

        if (frame.getName().equals(frameName)) {

            //make the frame visible
            frame.setVisible(true);

            //focus the frame
            frame.requestFocus();

            //found
            return;

        }

    }
Directrix answered 24/9, 2009 at 0:43 Comment(0)
O
-1

I have searched many answers for getting name and i think this is the only easy solution

public static void main(String[] args) {
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            String name = actionEvent.getSource().toString();
            UserReaction(ObjectName.getComponentVariableName(name), "null");
        }
    };
    Button calculate_btn = new Button("Calculate");
    calculate_btn.setName("Calculate");
    calculate_btn.addActionListener(actionListener);
}

private static void UserReaction(String objectName) {
    if (objectName.equals("Calculate")) {
        //do something;         
    }
}static public String getComponentVariableName(String name) {
    String res = (name.substring(name.indexOf("[") + 1));
    res = res.split(",")[0];
    return res;
}
Orcus answered 22/3, 2017 at 16:5 Comment(1)
This doesn't answer the question at all?Artimas

© 2022 - 2024 — McMap. All rights reserved.