Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 361
JohnnyLambada Avatar asked Oct 22 '08 19:10

JohnnyLambada


People also ask

What is AWT component in Java?

Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in java. Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components are using the resources of OS.

What does @component do in Java?

@Component is an annotation that allows Spring to automatically detect our custom beans. In other words, without having to write any explicit code, Spring will: Scan our application for classes annotated with @Component. Instantiate them and inject any specified dependencies into them.

What are components and containers in Java?

It is a container that holds components like buttons, textfield, etc. Creating an instance is the way to create a Panel Container and can add components. Frame: The Frame is a container used while creating an AWT application. It can have components like title bar, menu bars, borders and also buttons, scroll bar, etc.

Why are AWT components called heavyweight components?

Java Swing is implemented entirely in the Java programming language. First of all, by a heavy-weight, it means the code will take comparatively more time to load and it will consume more System resources. AWT is considered to be heavy-weight because its components are dependent on the underlying Operating System.


1 Answers

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?

like image 146
Herman Lintvelt Avatar answered Sep 20 '22 05:09

Herman Lintvelt