Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JLabel - add a unique identifier

Tags:

java

jlabel

Is there anyway to add an ID or a unique identifier to a JLabel?

Thanks all

like image 202
Abs Avatar asked Dec 06 '25 06:12

Abs


2 Answers

JLabel has a setName method that is inherited from java.awt.Component. You could use this for an ID.

like image 66
Mark Avatar answered Dec 07 '25 19:12

Mark


You can call Component.setName on it, or JComponent.putClientProperty, or subclass or use the JLabel object as a key is a Map of some sort.

But probably the way to go is to keep hold of a reference to the original object, so you can write clean, direct code that doesn't have to look the component up.

like image 41
Tom Hawtin - tackline Avatar answered Dec 07 '25 21:12

Tom Hawtin - tackline