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

后端 未结 9 1584
半阙折子戏
半阙折子戏 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

    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.

提交回复
热议问题