Переключение между тремя состояниями с помощью JToggleButton - PullRequest
2 голосов
/ 14 ноября 2011

Как я могу реализовать кнопку с тремя состояниями? Я использую eclipse и windowBuilder, и я пишу программу, которая должна иметь кнопки с 3 состояниями. Я использовал JToggleButtons и их два состояния для двух, но не могу реализовать третье состояние. Что я могу сделать?

P.S. Я хочу три значка состояния.

1 Ответ

1 голос
/ 14 ноября 2011

Может быть, вы можете посмотреть на код, используемый com.jidesoft.swing.TristateCheckBox:
См. TristateCheckBox.java.

Он наследуется от JToggleButton и имеет три разных состояния значка.

/**
 * 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 {
...