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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With