0

I know that subscribing for ItemListener on the checkbox one can get the ItemEvent.SELECTED/DESELECTED state inside the method:

public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange()==ItemEvent.DESELECTED){
    }
....
}

But what I want to know is the "last selection state" that the JComboBox during the state-change is actually happening. As JCheckBox extends JToggleButton , there must me some way to know that as the java buttons do have an intermediate state-value like "pressed" before it actuallly changed to the new state. But I coudn't find out the way to have it in case of JCheckBox

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
mediterranean
  • 123
  • 1
  • 9
  • 1
    I don't think there is a working solution other than saving the last state in variable. – Ben Mar 16 '18 at 12:50
  • 1
    if you know what the current state is, then you alsow know the previous state, because there are only 2 possible states – XtremeBaumer Mar 16 '18 at 12:51
  • XtremeBaumer I want to know the state before it actually change to the next one – mediterranean Mar 16 '18 at 13:10
  • 1
    i think you should tell us what you want to accomplish, because ther might be easier ways – XtremeBaumer Mar 16 '18 at 13:23
  • `I want to know the state before it actually change to the next one` - your question makes no sense. As has already been mentioned, **if you know the current state you know the previous state.** – camickr Mar 16 '18 at 14:35
  • @XtremeBaumer What I want to achieve is that I want to raise an advice (popup dialog) to the user of the application about the consequences when the checkbox is marked and he will attempt to unmark it. May be there is some other way around by it havn't occured to me so far. Thanks for your concern – mediterranean Mar 19 '18 at 07:19
  • 1
    Well, if the popup only occurs once the checkbox is marked, then the user still has to overthink what the consequences are and in turn he can also unmark it. there is no problem only showing the popup, once the checkbox is actually checked. its the same the other way around. just add an actionlistener which check the current state and dependant on that, the popup occurs – XtremeBaumer Mar 19 '18 at 07:21
  • @XtremeBaume yes, as you say that is the option I am willing to implement. But you know clients are always right against developers :) – mediterranean Mar 19 '18 at 11:58
  • 1
    You can implement a popup whwihc returns a boolean. If the user presses YES, then the checkbox will stay checked, but if he presses NO, you programmatically uncheck the checkbox. This way it looks like what you want – XtremeBaumer Mar 19 '18 at 12:08

1 Answers1

0

what about if I cast the event source to AbstractButton and then do as following:

ChangeListener changeListener = new ChangeListener() {
  public void stateChanged(ChangeEvent changeEvent) {
    AbstractButton abstractButton = (AbstractButton) 
    changeEvent.getSource();
    ButtonModel buttonModel = abstractButton.getModel();
    boolean armed = buttonModel.isArmed();
    boolean pressed = buttonModel.isPressed();
    boolean selected = buttonModel.isSelected();
    .......
  }
};
mediterranean
  • 123
  • 1
  • 9