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

时间:2014-04-20 00:04:12

标签: java combobox javafx

我需要一个显示客户名称的组合框,并在选择其中一个项目时给予customerId。组合框我意识到正确的方式工作,但当它失去焦点,或者我按下回车键时,我收到以下错误:

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)

以下是错误的行代码:( 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);
                }

            }

        });

这是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 ;
    }
}

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

1 个答案:

答案 0 :(得分:3)

您的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);
    }
});

至少我可以找到尝试重现错误的内容。

当然,防止出现错误的一种更简单的方法是使ComboBox不可编辑,但我认为您是故意编辑的。