简体   繁体   English

使用FX对象的JavaFX TableView

[英]JavaFX TableView using FX Objects

I am trying to put a stack panel in my TableView and I keep getting errors. 我试图在我的TableView放置一个堆栈面板,但不断出现错误。

I suspect it is my lack of understanding of creating the Cell Value Factory. 我怀疑这是我对创建Cell Value Factory缺乏了解。

My Code looks like: 我的代码如下所示:

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

And the Error I am getting is 我得到的错误是

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)

I am trying to convert from using a 我正在尝试从使用

public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> cdf) 

and only returning a SimpleStringProperty to returning a TableCell<ObservableList<String> , String> that has a StackPane as its content with the String held in a Label (I want to create actions based on clicking on the stack pane). 并且仅返回SimpleStringProperty以返回TableCell<ObservableList<String> ,String>,其中具有StackPane作为其内容,且String保留在Label (我想基于单击堆栈窗格来创建操作)。

Any help on understanding the transition would be great! 对理解过渡的任何帮助都将非常棒!

CellValueFactory is to populate data in cells of column from model object .. CellValueFactory将从模型对象中填充列单元格中的数据。

Example : 范例:

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

If you want to render data in all cells of table column then you must use cellFactory . 如果要在表列的所有单元格中呈现数据,则必须使用cellFactory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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