We have a couple of checkboxes and if any of them is selected we want to do something (say show a label on screen). I am reading this book and it suggests the following code but compiler gives the error "checkBox cannot be resolved to a variable".
Question 1- How can I solve this? Question 2- What's the difference between getItem and getSource methods of ItemEvent objects? (where to use which?).
public void itemStateChanged(ItemEvent ie) {
if( ie.getItem() == checkBox)) { //the book's suggestion but it doesn't work
if(ie.getStateChange() == ItemEvent.SELECTED) {
// statements that execute when the box is checked
} else {
// statements that execute when the box is unchecke
}
} else {
// statements that execute when the source of the event is
// some component other than the checkBox object
}
}
Aucun commentaire:
Enregistrer un commentaire