Reputation: 33
I have a JCheckBox
with action listener implemented.
Here, when it's checked, something appears and when not checked, the appearance disappears.
To implement this, should I have two action listeners? How do I implement this?
Upvotes: 2
Views: 97
Reputation: 1335
you can use ActionListener
for this and when that action is fired you can check it's selected or not
final JCheckBox checkBox = new JCheckBox("My checkbox");
checkBox.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// check if checkBox is selected or not
if(checkBox.isSelected()){
// here you can fire an event in which your checkbox is mark as selected
// and you can display the value you want to display
} else{
// checkBox is not selected so you can fire an event in which your checkbox is not selected
}
}
}
Upvotes: 3
Reputation: 347194
You have a single ActionListener
and you check the selected state of the JCheckBox
through the use of it's isSelected
property. A ActionListener
can not distinguish states on it's own, it simply responds to a user input
See How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners for more details.
Upvotes: 2