使用 FX 对象的 JavaFX TableView

标签 java uitableview user-interface javafx tableview

我正在尝试将堆栈面板放入我的 TableView 中我不断收到错误。

我怀疑是我对创建细胞值(value)工厂缺乏了解。

我的代码如下所示:

TableColumn obj = new TableColumn("Func " + i);
        final int index = i;
        obj.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>, String>, TableCell<ObservableList<String>, String>>() {



            @Override
            public TableCell<ObservableList<String>, String> call(final CellDataFeatures<ObservableList<String>, String> cdf) {
                if (index + 2 >= cdf.getValue().size()) {
                    return null;
                }
                //return new SimpleStringProperty(cdf.getValue().get(index + 2));
                return new TableCell<ObservableList<String>, String>(){
                    Label funcLbl = new Label(cdf.getValue().get(index+2));
                    StackPane sp = new StackPane();


                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty); //To change body of generated methods, choose Tools | Templates.
                        sp.getChildren().add(funcLbl);
                        setGraphic(sp);
                    }
                };
            }
        });
        obj.setMinWidth(100);
        this.getColumns().add(obj);

我得到的错误是

SEVERE: javafx.scene.control.Control loadSkinClass Failed to load skin 'StringProperty [bean: PinTable[id=null, styleClass=table-view], name: skinClassName, value: com.sun.javafx.scene.control.skin.TableViewSkin]' for control PinTable[id=null, styleClass=table-view]
java.lang.ClassCastException: mapr.components.mainui.render.table.MyTable$3$1 cannot be cast to javafx.beans.value.ObservableValue
    at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:1209)
    at javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:1179)
    at javafx.scene.control.TableCell.updateItem(TableCell.java:543)
    at javafx.scene.control.TableCell.indexChanged(TableCell.java:105)
    at javafx.scene.control.TableCell$1.invalidated(TableCell.java:93)
    at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:155)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
    at javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:195)
    at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:161)
    at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:130)
    at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:163)
    at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:112)
    at com.sun.javafx.scene.control.skin.TableColumnHeader.resizeToFit(TableColumnHeader.java:619)
    at com.sun.javafx.scene.control.skin.TableColumnHeader$1.invalidated(TableColumnHeader.java:138)
    at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:155)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
    at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:195)
    at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:161)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:130)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
    at javafx.scene.Node.setScene(Node.java:661)
    at javafx.scene.Parent.computeDirtyScene(Parent.java:599)
    at javafx.scene.Parent.sceneChanged(Parent.java:612)
    at javafx.scene.Node$4.invalidated(Node.java:699)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
    at javafx.scene.Node.setScene(Node.java:661)
    at javafx.scene.Parent.computeDirtyScene(Parent.java:599)
    at javafx.scene.Parent.sceneChanged(Parent.java:612)
    at javafx.scene.Node$4.invalidated(Node.java:699)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
    at javafx.scene.Node.setScene(Node.java:661)
    at javafx.scene.Parent.computeDirtyScene(Parent.java:599)
    at javafx.scene.Parent.sceneChanged(Parent.java:612)
    at javafx.scene.Node$4.invalidated(Node.java:699)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
    at javafx.scene.Node.setScene(Node.java:661)
    at javafx.scene.Parent$1.onChanged(Parent.java:425)
    at com.sun.javafx.collections.VetoableObservableList.callObservers(VetoableObservableList.java:83)
    at com.sun.javafx.collections.ObservableListWrapper.setAll(ObservableListWrapper.java:303)
    at com.sun.javafx.collections.VetoableObservableList.setAll(VetoableObservableList.java:92)
    at com.sun.javafx.collections.ObservableListWrapper.setAll(ObservableListWrapper.java:314)
    at javafx.scene.control.Control.updateChildren(Control.java:947)
    at javafx.scene.control.Control.access$100(Control.java:70)
    at javafx.scene.control.Control$1.invalidated(Control.java:207)
    at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:129)
    at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
    at com.sun.javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:70)
    at javafx.scene.control.Control$1.set(Control.java:186)
    at javafx.scene.control.Control$1.set(Control.java:171)
    at javafx.scene.control.Control.loadSkinClass(Control.java:1047)
    at javafx.scene.control.Control.access$500(Control.java:70)
    at javafx.scene.control.Control$12.invalidated(Control.java:972)
    at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:127)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:161)
    at com.sun.javafx.css.StyleableStringProperty.set(StyleableStringProperty.java:71)
    at javafx.scene.control.Control$12.set(Control.java:964)
    at com.sun.javafx.css.StyleableStringProperty.applyStyle(StyleableStringProperty.java:59)
    at com.sun.javafx.css.StyleableStringProperty.applyStyle(StyleableStringProperty.java:31)
    at com.sun.javafx.css.StyleableProperty.set(StyleableProperty.java:70)
    at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:902)
    at javafx.scene.Node.impl_processCSS(Node.java:7415)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1146)
    at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.control.Control.impl_processCSS(Control.java:1154)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.Parent.impl_processCSS(Parent.java:1153)
    at javafx.scene.Node.processCSS(Node.java:7383)
    at javafx.scene.Node.processCSS(Node.java:7377)
    at javafx.scene.Node.processCSS(Node.java:7377)
    at javafx.scene.Scene.doCSSPass(Scene.java:446)
    at javafx.scene.Scene.access$3800(Scene.java:170)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2202)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:363)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:460)
    at com.sun.javafx.tk.quantum.QuantumToolkit$9.run(QuantumToolkit.java:329)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    at java.lang.Thread.run(Thread.java:724)

我正在尝试使用

进行转换
public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) 

并且只返回SimpleStringProperty返回 TableCell<ObservableList<String> , String> 具有 StackPane作为其内容 String举办于Label (我想根据单击堆栈 Pane 来创建操作)。

任何有关理解过渡的帮助都会很棒!

最佳答案

CellValueFactory 是从模型对象填充列单元格中的数据..

示例:

lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));

如果您想呈现表格列的所有单元格中的数据,则必须使用cellFactory

关于使用 FX 对象的 JavaFX TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20845197/

相关文章:

ios - 通过单击单元格中的取消按钮来删除该单元格,也可以通过 editActionsForRowAt 方法删除该单元格

java - Selenium Webdriver 和 SoapUI 有什么区别?

user-interface - Racket /GUI : responding to double-click and hover events

java - 控制台应用程序中基于选项卡的自动完成

java - 覆盖 hashCode() 和 equals() 方法

swift - 触摸 tableviewCell 时,只有部分单元格突出显示

ios - 调用 deleteRowsAtIndexPaths 时 UITableView 断言失败

java - 如何最好地从方法中的匿名类返回值?

java - getters和setters的junit测试方法

java - 遵循 AWS 教程时对 UnknownResourceException 进行故障排除