【问题标题】:activate and deactivate ComboBox激活和停用组合框
【发布时间】:2011-02-02 21:31:00
【问题描述】:

取消选中复选框时如何使组合框可用(反之亦然)

为什么我取消选中复选框后组合框仍然禁用?

choice [] = {"A","B","C"};
JComboBox a = new JComboBox(choice);

JCheckBox chk = new JCheckBox("choice");

...
a.addActionListener(this);
chk.addActionListener(this);
...

public void actionPerformed(ActionEvent e) {

   //disable the a comboBox when the checkBox chk was checked
  if(e.getSource()==chk)
    a.setEnabled(false);

  //enable the a comboBox when the checkBox chk was unchecked
  else if(e.getSource()!=chk)
    a.setEnabled(true);
}

【问题讨论】:

    标签: java swing combobox checkbox


    【解决方案1】:

    如果我理解正确,我认为您需要做的就是根据复选框的当前值更改组合框的启用状态:

    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==chk) {
            a.setEnabled(chk.isSelected());
        } 
    }
    

    【讨论】:

    • 谢谢,这正是我需要的 :-)
    【解决方案2】:

    我做了这个并且工作了..

    public class JF extends JFrame implements ActionListener {
     String choice [] = {"A","B","C"};
     JComboBox a = new JComboBox(choice);
    
     JCheckBox chk = new JCheckBox("choice");
    
     JF()
     {
      this.add(a, BorderLayout.NORTH);
      this.add(chk, BorderLayout.CENTER);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      a.addActionListener(this);
      chk.addActionListener(this);
     }
    
     public void actionPerformed(ActionEvent e) {
    
        //NOTE THE FOLLOWING LINE!!!!
       if(e.getSource()==chk)
         a.setEnabled(chk.isSelected());
     }
     public static void main(String[] args) {
      new JF().setVisible(true);
     }
    }
    

    您的旧代码不起作用,因为即使取消选中复选框也会触发事件。触发器的来源是复选框。所以在选中和取消选中事件源时都是chk

    【讨论】:

      【解决方案3】:

      我有一个类似的设置,我使用了一个项目监听器,如下所示:

      CheckBox.addItemListener(new ItemListener() {
          public void itemStateChanged(ItemEvent e) {
              if(e.getStateChange()==ItemEvent.SELECTED){
                  ComboBox.setEnabled(true);
              }else if(e.getStateChange()==ItemEvent.DESELECTED){
                  ComboBox.setSelectedIndex(-1);
                  ComboBox.setEnabled(false);
              }
          }
      });
      

      这样选择和取消选择时的行为是不同的。

      【讨论】:

        【解决方案4】:
        if (e.getSource() == chckbxModificar) {
                if (chckbxModificar.isSelected()) {
                    cbxImpuesto.setEnabled(true);
                    cbxMoneda.setEnabled(true);
                    txtPorcentaje.setEditable(true);
                    txtSimbolo.setEditable(true);
        
                } else {
                    cbxImpuesto.setEnabled(false);
                    cbxMoneda.setEnabled(false);
                    txtPorcentaje.setEditable(false);
                    txtSimbolo.setEditable(false);
                }
            }
        

        【讨论】:

          猜你喜欢
          • 2021-11-07
          • 2014-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多