使用FX Objects的JavaFX TableView

时间:2013-12-30 18:23:26

标签: java uitableview user-interface javafx tableview

我正在尝试在我的TableView中放置一个堆栈面板并且我一直在收到错误。

我怀疑是我对创建Cell Value Factory缺乏了解。

我的代码如下:

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>,字符串&gt;其StackPane内容为StringLabel保存在{{1}}中(我想基于点击堆栈窗格创建操作)。

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

1 个答案:

答案 0 :(得分:1)

CellValueFactory 用于填充模型对象中列的单元格中的数据。

示例:

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

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