Search code examples
javaeclipsetogglebuttonwindowbuilder

Toggle Between Three State With JToggleButton


How can i implement a 3-state toggle button? I'm using eclipse and windowBuilder and i'm coding a program which has to have 3-state buttons. I used JToggleButtons and their two state for two bu can't implement the third state. What can i do?

P.S. I want three icon state.


Solution

  • Maybe you can look at the code used by com.jidesoft.swing.TristateCheckBox:
    See TristateCheckBox.java.

    It does inherit from JToggleButton and has three different icon states.

    /**
     * Maintenance tip - There were some tricks to getting this code
     * working:
     * <p/>
     * 1. You have to overwite addMouseListener() to do nothing
     * 2. You have to add a mouse event on mousePressed by calling
     * super.addMouseListener()
     * 3. You have to replace the UIActionMap for the keyboard event
     * "pressed" with your own one.
     * 4. You have to remove the UIActionMap for the keyboard event
     * "released".
     * 5. You have to grab focus when the next state is entered,
     * otherwise clicking on the component won't get the focus.
     * 6. You have to make a TristateDecorator as a button model that
     * wraps the original button model and does state management.
     *
     * @author Dr. Heinz M. Kabutz
     */
    public class TristateCheckBox extends JCheckBox {