使用Double Value编辑JFX JTable

时间:2016-05-04 09:45:34

标签: javafx

您好我正在学习JFX和trin编写一个简单的jtable示例,我在运行应用程序时遇到错误

我的代码段错误来自

itemPrice.setCellValueFactory(new PropertyValueFactory<Item, Double>("itemPrice"));
itemPrice.setCellFactory(TextFieldTableCell.forTableColumn());
itemPrice.setOnEditCommit(
        new EventHandler<TableColumn.CellEditEvent<Item, Double>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<Item, Double> t) {
                ((Item) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                ).setItemPrice(t.getNewValue());
            }

错误是

Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
    at javafx.util.converter.DefaultStringConverter.toString(DefaultStringConverter.java:34)
    at javafx.scene.control.cell.CellUtils.getItemText(CellUtils.java:100)
    at javafx.scene.control.cell.CellUtils.updateItem(CellUtils.java:201)
    at javafx.scene.control.cell.TextFieldTableCell.updateItem(TextFieldTableCell.java:204)
    at javafx.scene.control.TableCell.updateItem(TableCell.java:663)
    at javafx.scene.control.TableCell.indexChanged(TableCell.java:468)
    at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.updateCells(TableRowSkinBase.java:523)
    at com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:147)
    at com.sun.javafx.scene.control.skin.TableRowSkin.<init>(TableRowSkin.java:64)
    at javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212)
    at javafx.scene.control.Control.impl_processCSS(Control.java:859)
    at javafx.scene.Node.processCSS(Node.java:9035)
    at javafx.scene.Node.applyCss(Node.java:9132)
    at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1957)
    at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1790)
    at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1872)
    at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2511)
    at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
    at javafx.scene.Parent.layout(Parent.java:1076)
    at javafx.scene.Parent.layout(Parent.java:1082)
    at javafx.scene.Parent.layout(Parent.java:1082)
    at javafx.scene.Parent.layout(Parent.java:1082)
    at javafx.scene.Scene.doLayoutPass(Scene.java:552)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
    at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:314)
    at com.sun.javafx.tk.Toolkit$$Lambda$216/252608964.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:313)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:340)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:525)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:505)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$400(QuantumToolkit.java:334)
    at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$42/820829455.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/1329190985.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

我知道这是由于java中的转换问题而duno怎么编辑处理程序在JFX中工作的字符串数据类型的相同代码如何修复,帮助/提示请

1 个答案:

答案 0 :(得分:1)

使用

itemPrice.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));