JavaFX如何取消ComboBox选择更改?

时间:2016-09-23 07:38:57

标签: java javafx combobox

以下是演示代码:

public class ComboBoxTest extends Application {

    public static void main(String[] args) {
        Application.launch(ComboBoxTest.class);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.getItems().addAll("option1", "option2", "option3");
        comboBox.getSelectionModel().select(0);

        comboBox.getSelectionModel().selectedItemProperty().addListener(
            (_ob, _old, _new) -> {
                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");
    }
}

当更改comboBox的选择时,它会引发StackOverFlowError,我知道原因(ChangeListener对象一次又一次触发),但我不知道知道如何正确&#34;取消&#34; 此选择更改操作。

更新

对不起@fabian我在这里写了一个不好的例子,让我稍微改变一下:

private static boolean isValidChange(String _old, String _new) {
    return !_old.equals("option1");
}

现在可以使用,comboBox值无法更改。但是我在这里得到一个奇怪的IndexOutOfBoundsException,而堆栈跟踪中没有我的代码行:

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)

这是JavaFX的错误吗?

1 个答案:

答案 0 :(得分:0)

这里有两个问题。

  1. 从值a到值b的更改无效,但从ba的更改无效。因此,当值更改时,侦听器撤消更改,这会导致侦听器尝试撤消的另一个更改事件,侦听器再次尝试撤消,...

  2. 您可以修改选择模型。但是,选择模型依赖于选择不变。

  3. 要解决这些问题,请引入一个ChangeListener,以便知道它何时否决更改&#34;并在使用Platform.runLater处理更改事件后运行更改:

    public abstract class VetoListener<T> implements ChangeListener<T> {
    
        private final SelectionModel<T> selectionModel;
        private boolean changing = false;
    
        public VetoListener(SelectionModel<T> selectionModel) {
            if (selectionModel == null) {
                throw new IllegalArgumentException();
            }
            this.selectionModel = selectionModel;
        }
    
        @Override
        public void changed(ObservableValue<? extends T> observable, T oldValue, T newValue) {
            if (!changing && isInvalidChange(oldValue, newValue)) {
                changing = true;
                Platform.runLater(() -> {
                    selectionModel.select(oldValue);
                    changing = false;
                });
            }
        }
    
        protected abstract boolean isInvalidChange(T oldValue, T newValue);
    
    }
    

    以下代码可防止从"A"更改为"B"以及从"B"更改为"C"

    @Override
    public void start(Stage primaryStage) {
        ComboBox<String> combo = new ComboBox<>(FXCollections.observableArrayList("A", "B", "C"));
    
        combo.getSelectionModel().selectedItemProperty().addListener(new VetoListener<String>(combo.getSelectionModel()) {
    
            @Override
            protected boolean isInvalidChange(String oldValue, String newValue) {
                return oldValue != null && newValue != null
                        && !oldValue.isEmpty()
                        && !newValue.isEmpty()
                        && oldValue.charAt(0)+1 == newValue.charAt(0);
            }
    
        });
    
        Scene scene = new Scene(combo);
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }