将侦听器添加到 ComboBox#box.getSelectionModel()#selectedItemProperty() 会导致异常,因为 Java 8u72

Adding a listener to ComboBox#box.getSelectionModel()#selectedItemProperty() causes Exception since Java 8u72

如果您从 ComboBox 下拉列表中选择一个项目,然后按另一个控件,在本例中为 TextField,则下面的代码将生成 Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.String cannot be cast to MCVE$ComboBoxItem 异常。最奇怪的是,这是由以下代码引起的:

box.getSelectionModel().selectedItemProperty().addListener((o, oldVal, newVal) -> {
                // This is what causes  the issue. You don't even need to put anything here.
            });

尽管它实际上什么也没做。

我已经在多个平台和不同的 java 版本上对此进行了测试。而且这个错误似乎取决于 Java 版本。程序在Java 8u60、8u66、8u72测试,bug只出现在Java 8u72.

MCVE:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MCVE extends Application {
    @Override
    public void start(Stage stage) {
        ComboBox<ComboBoxItem> box = new ComboBox<ComboBoxItem>();
        box.setEditable(true);

        ObservableList<ComboBoxItem> items = FXCollections.observableArrayList(new ComboBoxItem("Option 1"),
                new ComboBoxItem("Option 2"));
        box.setItems(items);

        box.getSelectionModel().selectedItemProperty().addListener((o, oldVal, newVal) -> {
            // This is what causes  the issue. You don't even need to put anything here.
        });

        TextField textfield = new TextField();

        HBox root = new HBox();
        root.getChildren().addAll(box, textfield);

        stage.setScene(new Scene(root));

        stage.show();
    }

    public class ComboBoxItem {
        private String text;

        public ComboBoxItem(String text) {
            this.text = text;
        }

        @Override
        public String toString() {
            return text;
        }
    }

    public static void main(String[] args) {
        launch();
    }
}

完全异常:

Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.String cannot be cast to MCVE$ComboBoxItem
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
    at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
    at javafx.scene.control.ComboBox.lambda$new2(ComboBox.java:249)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:150)
    at com.sun.javafx.scene.control.skin.ComboBoxPopupControl.setTextFromTextFieldIntoComboBoxValue(ComboBoxPopupControl.java:405)
    at com.sun.javafx.scene.control.skin.ComboBoxPopupControl.lambda$new1(ComboBoxPopupControl.java:82)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72)
    at javafx.scene.Node$FocusedProperty.notifyListeners(Node.java:7718)
    at javafx.scene.Scene.invalidated(Scene.java:2077)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
    at javafx.scene.Scene$KeyHandler.setFocusOwner(Scene.java:3924)
    at javafx.scene.Scene$KeyHandler.requestFocus(Scene.java:3971)
    at javafx.scene.Scene$KeyHandler.access00(Scene.java:3910)
    at javafx.scene.Scene.requestFocus(Scene.java:2044)
    at javafx.scene.Node.requestFocus(Node.java:7879)
    at com.sun.javafx.scene.control.behavior.TextFieldBehavior.mousePressed(TextFieldBehavior.java:248)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:95)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.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.access00(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:352)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent4(GlassViewEventHandler.java:388)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
    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$null8(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

问题的原因不是添加侦听器,而是使用可编辑的 ComboBox 而没有适当的 StringConverter

这当然不是错误;它甚至被记录在案 in the javadoc:

By default the converter simply returns the String input as the user typed it, which therefore assumes that the type of the editable ComboBox is String. If a different type is specified and the ComboBox is to be editable, it is necessary to specify a custom StringConverter.

我猜听众只是 make 导致异常,因为 属性 不能再被惰性评估或类似的东西...

您可以像这样使用 StringConverter

    box.setConverter(new StringConverter<ComboBoxItem>() {

        @Override
        public String toString(ComboBoxItem object) {
            return object == null ? null : object.toString();
        }

        @Override
        public ComboBoxItem fromString(String string) {
            return box.getItems().stream().filter(item -> item.text.equals(string)).findFirst().orElse(null);
        }
    });