I want to create a (simple, hopefully) custom Swing component by composing several existing components. In my case, it is an on-off switch which consists of a JLabel, and two JButtons for On and Off. I begin OnOffSwitch by extending JPanel. The constructor adds the sub-components, and sets itself up as an ActionListener for the buttons. The class has an isOn() method for querying the current state of the component.
I now want to add the ability to add ActionListeners to the OnOffSwitch class. I expected this functionality would come for free by having extended a Swing component like JPanel, but JPanel does not have this functionality. By the looks of the sources, every Swing component which does have this functionality re-implements it itself: the adding listeners to the list, the firing of ActionEvents, etc.
What is the correct way to achieve what I want? I can copy/paste that code from the various Swing components (or re-write the gist of it), or I can implement my own OnOffSwitchListener interface. To be consistent it seems that all my components should use ActionListeners.