【问题标题】:Adding a listener to ComboBox#box.getSelectionModel()#selectedItemProperty() causes Exception since Java 8u72将侦听器添加到 ComboBox#box.getSelectionModel()#selectedItemProperty() 会导致 Java 8u72 以来的异常
【发布时间】:2016-05-06 00:34:02
【问题描述】:

如果您从ComboBox 下拉列表中选择一个项目,然后按另一个控件,则下面的代码将生成Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.String cannot be cast to MCVE$ComboBoxItem 异常,在本例中为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中测试,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$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)

【问题讨论】:

    标签: java exception javafx version listener


    【解决方案1】:

    问题的原因不是添加侦听器,而是使用了可编辑的 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);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2011-07-06
      • 2013-02-17
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多