我需要一个组合框来显示客户的姓名,并在选择其中一项时提供 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新手,所以我很确定这是我的错误,但我无法找到它。我们将不胜感激您的建议。
最佳答案
您的 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
不可编辑,但我想您是故意将其设置为可编辑的。
关于Java FXML ComboBox 值 java.lang.ClassCastException : java. lang.String 无法转换为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23176986/