While Swing components can have a name (http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#getName()) this is not an id; it is up to you to ensure consistency.
This name
property is not widely used. In SwingUtilities
there is no method for finding a child by name, just an ancestor: SwingUtilities#getAncestorNamed.
You would have to to this by yourself. In general it is not impossible. You would start with a Container
, iterate over its children (Component[] getComponents
) and check for the name. Do this recursively for every child that is a Container
itself. Break when found.
But this is untypical for Swing. The framework is not used declarative. (Other Java GUI toolkits are or can, like GWT or Android's native UI.) You would just store a reference to the component you create programmatically for later use.
Of course you can't search by variable name in Java. The name doesn't exist at runtime. Java works different than JavaScript. Most of the time it is not a good idea to directly transfer concepts from one system to another. You need to get used to each one to utilise it fully.