向ComboBox添加侦听器#box.getSelectionModel()#selectedItemProperty()导致自Java 8u72以来的异常

时间:2016-01-28 14:56:28

标签: java exception javafx version listener

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

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

即使它几乎没有做任何事情。

我已在多个平台上测试过这个版本,并使用不同的java版本。而这个bug似乎取决于Java版本。该程序在Java 8u60,8u66和8u72中进行了测试,该错误仅发生在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$new$152(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$new$291(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$13.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.access$1900(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$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.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: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$handleMouseEvent$354(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$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:2)

问题的原因是没有添加听众,而是使用可编辑的ComboBox而没有合适的StringConverter

这当然不是一个错误;它甚至记录了in the javadoc

  

默认情况下,转换器只会在用户输入时返回String输入,因此假设可编辑ComboBox的类型为String。如果指定了其他类型并且ComboBox可编辑,则需要指定自定义StringConverter

我想侦听器只会导致异常,因为该属性不能再进行延迟评估或类似的事情......

你可以像这样使用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);
        }
    });