I'm trying to write an undo manager for JToggleButton
s (specifically JCheckBox
es). Each check box has an item listener that adds a new edit to an undo manager. However, when I try to undo the selection, the setSelected(boolean)
method triggers the item listener, adding the undo as another UndoableEdit
, so undos can be made indefinitely. My workaround to this problem was this:
ItemListener[] ils = button.getItemListeners();
Arrays.stream(ils).forEach(button::removeItemListener);
button.setSelected(wasSelected);
Arrays.stream(ils).forEach(button::addItemListener);
This works, but seems a bit messy. Is there a better way to do this?