JavaFX: TableView не может редактировать с помощью FXML

В настоящее время я разрабатываю приложение JavaFX с FXML, которое включает в себя TableView, Я не мог понять, как использовать StringConverter<T> в TextFieldTableCell<S,T>, Я сделал немного кода до сих пор. Возвращает исключение, когда значение texfield сохраняется в ячейке;

Это мой источник...

    @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)

1 ответ

Я нашел решение. Это мое решение...

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());
                    }
                }
        );

Еще одно простое решение

columnID.setCellFactory(TextFieldTableCell.forTableColumn());
Другие вопросы по тегам