[Solved] What to do when no base class exists to a certain common interface in Java Swing


You can use the interface and create wrappers for each component type you need. JTextFieldSupportWrapper and JComboboxSupportWrapper both taking an instance of the wrapped object type and and delegating to the addActionListener methods.

abstract class ActionListenerSupportWrapper<T extends JComponent> 
    implements ActionListenerSupport
{
    protected final T comp;
    protected ActionListenerSupportWrapper(T comp) {
        this.comp = comp;
    }
 }

// for each supported component type
class JTextFieldSupportWrapper extends ActionListenerSupportWrapper<JTextField> {
    void addActionListener(ActionListener l) {
        comp.addActionListener(l);
    }
}

Now you could declare a map using the interface ActionListenerSupport and add wrapped instances into that map i’ll take a list for now.

List<ActionListenerSupport> l = // init
JTextField tf = // lookup
l.add(new JTextFieldSupportWrapper(tf));

hope this helps

I think this post can be improved with reflection not having to create the subtypes on your own. But may be you loose some type safety at compile time.

solved What to do when no base class exists to a certain common interface in Java Swing