gpt4 book ai didi

Javafx - 我无法将数据获取到 TableView 中

转载 作者:太空宇宙 更新时间:2023-11-04 10:55:18 26 4
gpt4 key购买 nike

我无法使用自己的数据填充 JavaFX TableView 对象。我试图修改the code found here以满足我的程序的需要。我添加了该教程中使用的表格,并且它显示正常。我复制了该代码来创建第二个表,但无法让我的数据显示在第二个表中。

我相信我已经正确修改了代码以接受来自 SNMPInterface 类的数据。我尝试使用静态数据填充表,然后使用从文件中读取的数据填充表。这两个过程都不​​起作用,但都会创建具有正确标题的列。

My full project can be found on GitHub .

首先,我创建“SNMPInterface”类对象的 TableView 对象:

private TableView< SNMPInterface > interfaceTableView = new TableView<>();

然后我创建一个 SNMPInterface 对象的 ObservableList:

private final ObservableList< SNMPInterface > interfaceData =
FXCollections.observableArrayList(
new SNMPInterface( "99", "testlo" ),
new SNMPInterface( "98", "testeth1" ),
new SNMPInterface( "97", "testeth2" ),
new SNMPInterface( "96", "testbond0" )
);

稍后,我为“ifIndex”数据成员创建一列:

TableColumn< SNMPInterface, String > ifIndexCol = new TableColumn<>( "Index" );
ifIndexCol.setCellValueFactory( new PropertyValueFactory<>( "ifIndex" ) );

...以及“ifDecr”的第二列:

TableColumn ifDescrCol = new TableColumn( "Description" );
ifDescrCol.setCellValueFactory( new PropertyValueFactory<>( "ifDescr" ) );

然后我尝试将其添加到 GridPane(名为 rootNode):

interfaceTableView.setItems( interfaceData );
interfaceTableView.getColumns().setAll( ifIndexCol, ifDescrCol );
rootNode.add( interfaceTableView, 0, 7, 2, 1 );

...但这不起作用。

我有一个循环来验证数据是否可供该方法使用,第二个循环则验证数据是否已从文件中正确读取。两个容器似乎都有有效数据,但都没有进入我的表。

我的表格似乎与教程表格实际上相同,但显然我在某处犯了错误。有人看到我的错误在哪里吗?

最佳答案

用于输入 PropertyValueFactory 的 SNMPInterface 类上的 getter 和 setter应标记为公开,而不是 no modifier (否则 PropertyValueFactory 固有的反射逻辑将找不到它们)。

public static class SNMPInterface {
private final SimpleStringProperty ifIndex;
private final SimpleStringProperty ifDescr;

SNMPInterface( String ifIndex, String ifDescr ) {
this.ifIndex = new SimpleStringProperty( ifIndex );
this.ifDescr = new SimpleStringProperty( ifDescr );
}

public String getIfIndex() {
return ifIndex.get();
}

public void setIfIndex( String index ) {
ifIndex.set( index );
}

public String getIfDescr() {
return ifDescr.get();
}

public void setIfDescr( String descr ) {
ifDescr.set( descr );
}
}

关于Javafx - 我无法将数据获取到 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com