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 p
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;
}
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.
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.