I have a Swing app with multiple JCheckBoxes. Each JCheckBox corresponds to another object of type Foo have elsewhere in my program. For each object of type Foo, I’ll have one of these JCheckBoxes available for the user to click on (it tells the Foo object if they should display on the UI or not).
What I am currently doing is using the name attribute on JCheckBox as a reference back to the string “name” of the Foo object. I feel like a much cleaner solution would be to extend JCheckBox to add an extra field to it, so that the JCheckBox can keep track of which Foo object it’s attached to by simply storing a reference to it. Thoughts? Do people regularly extend Swing components so that they can link the parts in the controller/view back to the model? Is there some horrible pitfall I’m going to run into?
It is hard to get the whole picture for this particular example without additional details. You may be able to accomplish this task using actions. See How to Use Actions for more details. Here is an example: