JavaFX如何取消ComboBox选择更改?

标签 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对象被一次又一次触发),但我不知道如何正确“取消”这个选择更改操作。

<小时/>

更新:

抱歉@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 的错误吗?

最佳答案

这里有 2 个问题。

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

  2. 您修改选择模型。然而,选择模型依赖于选择保持不变。

要解决这些问题,请引入一个 ChangeListener,它“知道何时否决更改”,并在使用 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();
}

关于JavaFX如何取消ComboBox选择更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655280/

相关文章:

Java Fx 如何引用类中添加的元素

html - Wicket:带有用于多选的复选框的下拉框

WPF,MVVM 使用另一个组合框所选项目填充组合框

java - Android - onTouchListener 不适用于子 LinearLayout

java - 使用 JPA2 进行多重联接

java - 线分割算法

css - javafx2.2制作背景透明的滚动条

java - 如何限制javafx垂直flowpane中的列数

c# - 组合框在 SelectionChanged 事件中仍然具有旧值

java - Maven 忽略 OSX 上的 JAVA_HOME?