views:

80

answers:

2

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);
}
+2  A: 

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());
    } 
}
ninesided
Thank you, this is what I need :-)
Jessy
A: 

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

echo
Thanks raj :-) ..
Jessy