I add a listener to JCheckBox component and I want call listener manually.how do it?
myCheckBox.selected(false)
then I want to called myCheckBox listener. Do you have better idea?
I know I'm a bit late, but this should do the trick:
ItemListener listener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
//whatever your itemStateChanged() looks like.
}
};
JCheckBox checkBox = new JCheckBox();
checkBox.addItemListener(listener);
Then, whenever you need to call it manually:
listener.itemStateChanged(
new ItemEvent(checkBox, ItemEvent.ITEM_STATE_CHANGED, checkBox, 0));
If you created your listener anonymously, you can still access it like:
checkBox.getItemListeners()[0].itemStateChanged(
new ItemEvent(checkBox, ItemEvent.ITEM_STATE_CHANGED, checkBox, 0));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With