Search code examples
javaswingoverridingjcheckbox

javax.swing.JCheckBox setSelected is not calling from GUI


I'm trying to extend some Swing components and override methods that interacts with its state (setSelectedIndex of JComboBox, setSelected of JCheckBox, etc). There are problem with JCheckBox. I've override setSelected method, but it seems it does not call when user changes JCheckBox state via UI. I can change JCheckBox state by calling method but it seems that GUI uses another way to change it's state. How can I catch that event in my class? With other classes of Swing everything is alright and all overridden methods working correctly.

public class MyCheckBox extends JCheckBox {
    @Override
    public void setSelected(boolean selected) {
        //Method is not performed when MyCheckBox is clicked
        super.setSelected(selected);
    }
}

UP.: I've made it to have package of components that support 'undo/redo' actions. I just added addUndoableEditListener(UndoableEditListener l) method to all components, so implementations is hidden inside my components. That is why I extend Swing components instead of using action listeners.


Solution

  • You need to add an ActionListener to the checkbox.