我目前正在使用包含TableView
的FXML开发JavaFX应用程序。我无法弄清楚如何在StringConverter<T>
中使用TextFieldTableCell<S,T>
。到目前为止,我已经完成了一些代码。当texfield值保存到单元格时,它返回一个Exception;
这是我的来源......
@FXML
private TableColumn<Customer, Integer> columnID;
columnID.setCellFactory(
new Callback<TableColumn<Customer, Integer>, TableCell<Customer, Integer>>() {
StringConverter<Integer> converter;
@Override
public TableCell<Customer, Integer> call(TableColumn<Customer, Integer> list) {
return new TextFieldTableCell<>(converter);
}
}
);
它返回此异常......
Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: Attempting to convert text input into Object, but provided StringConverter is null. Be sure to set a StringConverter in your cell factory.
at javafx.scene.control.cell.CellUtils$4.handle(CellUtils.java:247)
at javafx.scene.control.cell.CellUtils$4.handle(CellUtils.java:243)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
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.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.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)
答案 0 :(得分:3)
我找到了解决方案。这是我的解决方案......
columnID.setCellFactory(
new Callback<TableColumn<Customer, Integer>, TableCell<Customer, Integer>>() {
StringConverter<Integer> converter;
@Override
public TableCell<Customer, Integer> call(TableColumn<Customer, Integer> list) {
return new TextFieldTableCell(new NumberStringConverter());
}
}
);
答案 1 :(得分:0)
另一个简单的解决方案
columnID.setCellFactory(TextFieldTableCell.forTableColumn());