简体   繁体   中英

JavaFX: TableView couldn't edit with FXML

I am currently developing a JavaFX Application with FXML that includes a TableView . I could not figure out how to use StringConverter<T> in the TextFieldTableCell<S,T> . I've done some code so far. It returns an Exception when texfield value saving to the cell;

This is my source...

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

And it returns this Exception...

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)

I found the solution. This is my solution...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM