Java FXML ComboBox值java.lang.ClassCastException:无法将java.lang.String强制转换为

0 java combobox javafx

我需要一个组合框,用于显示客户名称,并在选择其一项时提供customerId。我意识到的组合框以正确的方式工作,但是当它失去焦点时,或者按Enter键时,出现以下错误:

Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.String cannot be cast to kinema.classi.NomeCodice
    at kinema.ASediController$1.changed(ASediController.java:104)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:347)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176)
    at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
    at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
    at javafx.scene.control.ComboBox$2.changed(ComboBox.java:221)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:347)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    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:145)
    at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:167)
    at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.setTextFromTextFieldIntoComboBoxValue(ComboBoxListViewSkin.java:512)
    at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.access$200(ComboBoxListViewSkin.java:57)
    at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin$3.handle(ComboBoxListViewSkin.java:162)
    at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin$3.handle(ComboBoxListViewSkin.java:150)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventFilterRecord.handleCapturingEvent(CompositeEventHandler.java:282)
    at com.sun.javafx.event.CompositeEventHandler.dispatchCapturingEvent(CompositeEventHandler.java:98)
    at com.sun.javafx.event.EventHandlerManager.dispatchCapturingEvent(EventHandlerManager.java:223)
    at com.sun.javafx.event.EventHandlerManager.dispatchCapturingEvent(EventHandlerManager.java:180)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchCapturingEvent(CompositeEventDispatcher.java:43)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:52)
    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:204)
    at javafx.scene.Scene$KeyHandler.process(Scene.java:3949)
    at javafx.scene.Scene$KeyHandler.access$2100(Scene.java:3896)
    at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2036)
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2493)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:170)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:123)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:197)
    at com.sun.glass.ui.View.handleKeyEvent(View.java:517)
    at com.sun.glass.ui.View.notifyKey(View.java:927)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:744)
Run Code Online (Sandbox Code Playgroud)

这是错误的行代码:( ASediController.java:104行代码)。

ComboAzienda.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<NomeCodice>() {
            @Override
            public void changed(ObservableValue<? extends NomeCodice> selected,
                    NomeCodice VecchiaAzienda, NomeCodice NuovaAzienda) {

                try {
                    CaricaAzienda(NuovaAzienda.getIdCodice());
                } catch (SQLException ex) {
                    Logger.getLogger(ASediController.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        });
Run Code Online (Sandbox Code Playgroud)

这是Nomecodice类别:

public class NomeCodice {
      private  int TIdCodice; 
     private  String TDenominazione ;


    public NomeCodice(String Denominazione,int IdCodice) {
        this.TDenominazione = Denominazione;         
        this.TIdCodice = IdCodice;             
    }  


    public int getIdCodice() {
        return TIdCodice;
    }
    public void setIdCodice(int fCodice) {
        TIdCodice= fCodice;
    }         
    public String getTDenominazione() {
        return TDenominazione;
    }
    public void setTDenominazione(String fDenominazione) {
        TDenominazione = fDenominazione;
    }      


      @Override
       public String toString() {
       return TDenominazione ;
    }
}
Run Code Online (Sandbox Code Playgroud)

我是Java的新手,所以我很确定这是我的错误,但是我无法找到它。您的建议将不胜感激。

fab*_*ian 6

ComboBox是可编辑的,但没有提供正确的StringConverter转换器ComboBox。像这样设置转换器属性:

ComboAzienda.setConverter(new StringConverter<NomeCodice>() {

    @Override
    public String toString(NomeCodice object) {
        if (object == null) return null;
        return object.toString();
    }

    @Override
    public NomeCodice fromString(String string) {
        // replace this with approquiate implementation of parsing function
        // or lookup function
        return new NomeCodice(string, 0);
    }
});
Run Code Online (Sandbox Code Playgroud)

至少我可以找到尝试重现您的错误的方法。

当然,防止错误的一种更简单的方法是将其设为ComboBox不可编辑,但我想您是故意将其设为可编辑的。