activate and deactivate ComboBox

How can I make the comboBox available when the checkBox was uncheck (vice versa)

Why the comboBox is still disable after I unChecked the checkBox?

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);
}

Answers


If I understand you correctly I think all that you need to do is to change the enabled state of the combo box based on the current value of the checkbox:

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

I have a similar set up, and I use an Item Listener, like so:

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);
        }
    }
});

This way the behaviour is different when selected and deselected.


I treid this and worked..

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);
 }
}

Your old code didn't work because, even unchecking a checkbox triggers the event. The source of the trigger is the checkbox.. so both while checking and unchecking the event source was chk


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);
        }
    }

Need Your Help

write contents that display in browser to a file php?

php file text

I want to write contents that display in the browser to a text file. I want to display the data both in browser and write to a text file. Here is my script. I used file_put_contents to write to tex...

carrierwave does not save content_type on Uploader

ruby-on-rails ruby carrierwave

I have followed the instructions from https://github.com/carrierwaveuploader/carrierwave#setting-the-content-type