如何设置JToggleButton的切换状态?

时间:2014-04-12 01:29:16

标签: java swing border jtogglebutton

我已经将一些JToggleButtons添加到带有BoxLayout的JPanel中。按钮已放置在ButtonGroup中。现在我想切换当前所选按钮的一些属性,只有在选中它时。我正在寻找这样的东西:

buttonGroup.addSomeListener(new SomeListener() {
    @Override
    public void whenToggled() {
        /*set attributes for button*/
    }
});

具体来说,我想让所选按钮显示BevelBorder.LOWERED而不是

button.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));

我的JPanel中的按钮有。

修改

此代码不会打印出“翻转”,除非我只是将button的类型更改为JButton JToggleButton如何在changelistener中监听mouseOver?

public class Test {

   public static void main(String[] args) {

  JPanel panel = new JPanel();
  final JToggleButton button = new JToggleButton();
  button.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent arg0) {
        ButtonModel mod = button.getModel();
        if (mod.isRollover()) {
            System.out.println("rollover");
        }
    }
  });
  JFrame frame = new JFrame();
  frame.add(panel);
  panel.add(button);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

} }

3 个答案:

答案 0 :(得分:1)

再一次,我自己,我会使用一个ItemListener。我还考虑将斜面边框与Toggle的复合边框边框结合起来,因为这样可以使按钮更大。例如:

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.border.*;

public class Foo {
   static CompoundBorder lowered; // = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
   static CompoundBorder raised; // = BorderFactory.createBevelBorder(BevelBorder.RAISED);

   public static void main(String[] args) {

      JPanel panel = new JPanel();

      final ButtonGroup btnGrp = new ButtonGroup();
      for (int i = 0; i < 4; i++) {
         JToggleButton toggle = new JToggleButton("Toggle " + (i + 1));
         if (lowered == null) {
            CompoundBorder toggleBorder = (CompoundBorder) toggle.getBorder();
            Border toggleInside = toggleBorder.getInsideBorder();
            lowered = BorderFactory.createCompoundBorder(
                  BorderFactory.createBevelBorder(BevelBorder.LOWERED),
                  toggleInside);
            raised = BorderFactory.createCompoundBorder(
                  BorderFactory.createBevelBorder(BevelBorder.RAISED),
                  toggleInside);
         }
         toggle.setBorder(raised);
         btnGrp.add(toggle);
         panel.add(toggle);
         toggle.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent iEvt) {
               Border border = iEvt.getStateChange() == ItemEvent.SELECTED ? lowered
                     : raised;
               ((JComponent) iEvt.getSource()).setBorder(border);
            }

         });
      }

      JOptionPane.showMessageDialog(null, panel);

   }

}

是的,我同意这不起作用:

     toggle.getModel().addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent evt) {
           ButtonModel model = (ButtonModel) evt.getSource();

           // this doesn't work
           if (model.isRollover()) {
              System.out.println(model.getActionCommand() + " is rolled over");
           }

           // but this does
           if (model.isSelected()) {
              System.out.println(model.getActionCommand() + " is selected");
           }
        }
     });

答案 1 :(得分:0)

猜猜我问得太快了。我在找ChangeListener。您希望将侦听器应用于按钮的ButtonModel

button.getModel().addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
        ButtonModel model = (ButtonModel) e.getSource();
        if (model.isSelected()) {
            button.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        } else {
            button.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        }
    }
});

答案 2 :(得分:0)

看看这个答案: Getting the state of JToggleButton

这是最简单的方法:

JToggleButton jtb = new JToggleButton("Press Me");
jtb.addItemListener(new ItemListener() {
   public void itemStateChanged(ItemEvent ev) {
      if(ev.getStateChange()==ItemEvent.SELECTED){
        System.out.println("button is selected");
      } else if(ev.getStateChange()==ItemEvent.DESELECTED){
        System.out.println("button is not selected");
      }
   }
});