栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我的表格视图中的表格单元为空。JavaFX + Scenebuilder

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我的表格视图中的表格单元为空。JavaFX + Scenebuilder

您的

get
方法名称错误。根据
PropertyValueFactory
文档,如果传入属性名称“
xyz”,则属性值工厂将首​​先
xyzProperty()
在表行中查找属于该对象的方法。如果找不到,将重新寻找一种称为
getXyz()
(仔细查看大写字母)的方法,然后将结果包装在中
ReadOnlyObjectWrapper

因此,以下方法将起作用:

package application;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.property.SimpleStringProperty;public class Table {    private final SimpleIntegerProperty bPlayerID;    private final SimpleStringProperty bLeague;    private final SimpleStringProperty bName;    public Table(int cPlayerID, String cLeague, String cName) {        this.bPlayerID = new SimpleIntegerProperty(cPlayerID);        this.bLeague = new SimpleStringProperty(cLeague);        this.bName = new SimpleStringProperty(cName);    }    public int getBPlayerID() {        return bPlayerID.get();    }    public void setBPlayerID(int v) {        bPlayerID.set(v);    }    public String getBLeague() {        return bLeague.get();    }    public void setBLeague(String v) {        bLeague.set(v);    }    public String getBName() {        return bName.get();    }    public void setBName(String v) {        bName.set(v);    }}

但是,如

PropertyValueFactory
文档中所述,这种情况下的属性将不是“活动的”:换言之,如果值发生更改,表将不会自动更新。此外,如果您想使表可编辑,则在没有进行显式连接以调用set方法的情况下,它不会更新属性。

最好使用“ 属性和绑定”教程中的大纲定义表模型:

package application;import javafx.beans.property.SimpleIntegerProperty;import javafx.beans.property.IntegerProperty;import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;public class Table {    private final IntegerProperty bPlayerID;    private final StringProperty bLeague;    private final StringProperty bName;    public Table(int cPlayerID, String cLeague, String cName) {        this.bPlayerID = new SimpleIntegerProperty(cPlayerID);        this.bLeague = new SimpleStringProperty(cLeague);        this.bName = new SimpleStringProperty(cName);    }    public int getBPlayerID() {        return bPlayerID.get();    }    public void setBPlayerID(int v) {        bPlayerID.set(v);    }    public IntegerProperty bPlayerIDProperty() {        return bPlayerID ;    }    public String getBLeague() {        return bLeague.get();    }    public void setBLeague(String v) {        bLeague.set(v);    }    public StringProperty bLeagueProperty() {        return bLeague ;    }    public String getBName() {        return bName.get();    }    public void setBName(String v) {        bName.set(v);    }    public StringProperty bNameProperty() {        return bName ;    }}

如果这样做,则(在Java 8中)可以使用以下单元格值工厂,而不是

PropertyValueFactory

aPlayerID.setCellValueFactory(cellData -> cellData.getValue().bPlayerIDProperty());

这将允许编译器捕获任何错误,而不仅仅是在运行时静默失败。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/494830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号