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

后端 未结 9 1585
半阙折子戏
半阙折子戏 2020-11-28 13:50

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

9条回答
  •  鱼传尺愫
    2020-11-28 13:55

    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?

提交回复
热议问题