Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associate custom data with a swing control?

Tags:

java

swing

awt

Is it possible to associate some custom (of type Object) data with Swing/AWT control?

I am creating multiple toggle buttons on a panel to select one of multiple objects, and want to remember, which object each button selects.

like image 222
Suzan Cioc Avatar asked Sep 01 '25 20:09

Suzan Cioc


2 Answers

In Swing, you can use putClientProperty for a JComponent. Maybe this can helps you: http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#putClientProperty%28java.lang.Object,%20java.lang.Object%29

Good luck!

like image 76
angel_navarro Avatar answered Sep 03 '25 09:09

angel_navarro


The way I often deal with situations like this is to store something like a HashMap<JButton, Object> as a class field and update it when a button is pressed. For toggle buttons, a toggle on could add the button/object pair to the map, and a toggle off could remove it. Or if you just want to track which objects have been selected, store an ArrayList<Object> and update it similarly.

like image 31
Josh Avatar answered Sep 03 '25 09:09

Josh