Java FXML ComboBox值java.lang.ClassCastException:java.lang.String不能转换为 [英] Java FXML ComboBox value java.lang.ClassCastException: java.lang.String cannot be cast to
问题描述
我需要一个组合框,显示客户的名称,并在选择其中一个项目时给出customerId。组合框我意识到工作正确的方式,但当它松开焦点,或我按下回车键,我得到以下错误:
线程JavaFX应用程序线程中的异常java.lang.ClassCastException:java.lang.String无法强制转换为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)
在javafx.beans.property.ReadOnlyObjectWrapper $ ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176)
在javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142)
在javafx.beans。 property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
在javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
在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)
在javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
在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)
在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)
在javafx.event.Event.fireEvent(Event.java:204)
在javafx.scene.Scene $ KeyHandler.process Scene.java:3949)
在javafx.scene.Scene $ KeyHandler.access $ 2100(Scene.java:3896)
在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方法)
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)
在java.lang.Thread.run(Thread.java :744)
这里是错误的行代码:( ASediController.java:104行代码) 。
ComboAzienda.getSelectionModel()。selectedItemProperty()。addListener(new ChangeListener< NomeCodice>(){
@重写
public void changed(ObservableValue< ;?扩展NomeCodice>选择,
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
是可编辑的,您不能为 ComboBox
提供正确的 StringConverter
转换器。设置转换器属性如下:
ComboAzienda.setConverter(new StringConverter< NomeCodice>(){
@Override
public String toString(NomeCodice object){
if(object == null)return null;
return object.toString();
}
b $ b @Override
public NomeCodice fromString(String string){
//用分析函数的替换实现替换它
//或查找函数
return new NomeCodice(string, 0);
}
});
至少这就是我想知道的重现你的错误。
当然,一个更简单的方法来防止你的错误是使 ComboBox
不可编辑,但我想你可以有意义地编辑。 / p>
I need a combobox that displays the name of a customer and give the customerId when one of its items is selected. The combobox i realized works in right way, but when it loose the focus, or I hit the enter key, I get the following error:
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)
Here is the line code of the error:( ASediController.java:104 line code).
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);
}
}
});
And This is the class 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 ;
}
}
I'm new in java, so i'm quite sure it'is a my error, but i'm not able ti find it out. Your suggestions will be appreciated.
Your ComboBox
is editable, but you don't provide a correct StringConverter
as converter for the ComboBox
. Set the converter property like this:
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);
}
});
At least that's what I could find out trying to reproduce your error.
Of course an easier way to prevent your error is to make the ComboBox
non-editable, but I suppose you made it editable on purpose.
这篇关于Java FXML ComboBox值java.lang.ClassCastException:java.lang.String不能转换为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!