When I check if my Checkbox
is selected or unselected, the function isSelected()
doesn't work.
I have this error: the method isSelected() is undefined for the type Checkbox
, and I don't know why.
Can you explain me where the problem is or tell me if there is an other solution to check if my Checkbox
is selected ?
Code:
import javax.swing.*;
import java.awt.event.*;
public class Example extends JFrame{
public JCheckBox one;
public Example() {
one = new JCheckBox("CT scan performed");
one.addItemListener(new CheckBoxListener());
setSize(300,300);
getContentPane().add(one);
setVisible(true);
}
private class CheckBoxListener implements ItemListener{
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==one){
if(one.isSelected()) {
System.out.println("one has been selected");
} else {System.out.println("nothing");}
}
}
}
public static void main(String[] args) {
new Example();
}
}