JavaFX-我无法将数据获取到TableView中 [英] Javafx - I cannot get data into a TableView

查看:82
本文介绍了JavaFX-我无法将数据获取到TableView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法用自己的数据填充JavaFX TableView对象.我试图修改在此处找到的代码以适应需要我的程序. 我添加了该教程中使用的表,并且该表可以正确显示.我复制了该代码以创建第二个表,但是无法让我的数据显示在第二个表中.

I am unable to populate a JavaFX TableView object with my own data. I have attempted to modify the code found here to suit the needs of my program. I added the table used in that tutorial, and it displays properly. I copied that code to create a second table, but cannot get my data to display in that second table.

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

I believe that I have properly modified the code to accept data from my SNMPInterface class. I attempt to populate my table with static data, and later with data read in from a file. Neither process works, though either will create the columns with the proper headers.

我的完整项目可以在GitHub上找到.

最初,我创建一个"SNMPInterface"类对象的TableView对象:

Initially, I create a TableView object of 'SNMPInterface' class objects:

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

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

I then create an ObservableList of SNMPInterface objects:

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

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

Later, I create a column for the 'ifIndex' data member:

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

...以及"ifDescr"的第二列:

...and the second column for 'ifDescr':

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

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

I then try to add it to the GridPane (named rootNode):

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

...但这不起作用.

...but that does not work.

我有一个循环来验证数据是否可用于该方法,还有一个循环来验证是否已从文件中正确读取数据.这两个容器似乎都有有效的数据,但都没有放入我的表中.

I have a loop to verify that the data is available to the method, and a second that verifies that the data is properly read in from the files. Both containers seem to have valid data, but neither makes it into my table.

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

My table seems to be effectively the same as the tutorial table, but obviously I am making an error somewhere. Does anyone see where my error is?

推荐答案

用于输入

The getters and setters on the SNMPInterface class that you use for input to PropertyValueFactory should be marked public, not no modifier (otherwise the reflection logic inherent in the PropertyValueFactory won't find them).

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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆