title: JavaFX how to cancel ComboBox selection change?
body: <p>Here is the demo code:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ComboBoxTest extends Application {
  
      public static void main(String[] args) {
          Application.launch(ComboBoxTest.class);
      }
  
      @Override
      public void start(Stage primaryStage) throws Exception {
          ComboBox&lt;String&gt; comboBox = new ComboBox&lt;&gt;();
          comboBox.getItems().addAll("option1", "option2", "option3");
          comboBox.getSelectionModel().select(0);
  
          comboBox.getSelectionModel().selectedItemProperty().addListener(
              (_ob, _old, _new) -&gt; {
                  if (!isValidChange(_old, _new)) {
                      // ERROR: try to cancel change and get StackOverflowError
                      comboBox.getSelectionModel().select(_old);
                  }
          });
  
          primaryStage.setScene(new Scene(
                  new BorderPane(comboBox), 300, 200));
          primaryStage.show();
      }
  
      private static boolean isValidChange(String _old, String _new) {
          // return false;
          return !_old.equals("option1");
      }
  }
  </code></pre>
  
  <p>When changing selection of <code>comboBox</code>, it centainly throws <code>StackOverFlowError</code>, I know the reason why (the <code>ChangeListener</code> object is triggered again and again), but what I don't know is how to correctly <strong>"cancel"</strong> this selection change operation.</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>Sorry @fabian I wrote a bad example here, let me change it a little bit:</p>
  
  <pre><code>private static boolean isValidChange(String _old, String _new) {
      return !_old.equals("option1");
  }
  </code></pre>
  
  <p>It works now, the <code>comboBox</code> value cannot be changed. However I get a strange <code>IndexOutOfBoundsException</code> here, while there is no line of my code in the stack trace:</p>
  
  <pre><code>Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException
      at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:136)
      at javafx.collections.ListChangeListener$Change.getAddedSubList(ListChangeListener.java:242)
      at com.sun.javafx.scene.control.behavior.ListViewBehavior.lambda$new$177(ListViewBehavior.java:269)
      at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
      at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
      at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
      at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.callObservers(ReadOnlyUnbackedObservableList.java:75)
      at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:378)
      at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1403)
      at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:256)
      at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:220)
      at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
      at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
      at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
      at com.sun.glass.ui.View.notifyMouse(View.java:937)
      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>Is this a bug of JavaFX?</p>