Reputation: 13598
What's the intended use of the name property of swing components? Is it used swing internally?
Background: A colleague implemented a internationalization mechanism by storing the key for the text string in the name property. Then, he simply walks through all swing-elements and gets the key stored in the name property of the component. He argued that the name property doesn't seem to be used otherwise and that this was the easiest way to do it.
Upvotes: 9
Views: 2808
Reputation: 10519
In my experience, I have never encountered any problem when setting the name of a Swing component. For "leaf" Swing components (that you use diretly, such as JLabel
, JButton
, JMenu
...), name
is always left null
by Swing.
As @kelopatra mentioned, inner components of "complex" Swing components (e.g. JColorChooser
) may have names assigned to them, but you generally cannot access these inner components directly (other than walking through the copmponent hierarchy tree).
As per uses of the name
property, it is often used for resource injection (i18n), but it can also be extremely useful for UI automation (for tests or demos), because most robots (e.g. FEST Swing) will be able to find a component by name, provided you assign unique names to your components.
Upvotes: 3
Reputation: 51535
Is it used swing internally?
Short answer: yes.
Longer answer: rather easy to verify - simply build some ui and walk the tree. Or look at f.i. the SwingLabs-Demo (can't resist :-),
Next question is: does that Swing internal setting interfere with setting the name for application reasons?
Short answer: hard to tell, most probably not
Longer answer: the internal settings I have seen are unlikely to be overwritten for application needs as they are deeply hidden in the container hierarchy. In fact, some frameworks like f.i. SAF does use the name for resource injection (similarly to what you describe your collegue is doing). My own (unmaintained) FormBuilder framework did for layout constraints.
The vague (read: undefined) definition of the name property is both an advantage and a trap:
Upvotes: 4
Reputation: 57421
Name of component from javadoc "Set or get the name of the component. This can be useful when you need to associate text with a component that does not display text.". So I think it's fine to use the name.
You can also place something in the component's properties.
Upvotes: 9