Adam Howell Adam Howell - 1 year ago 107
Java Question

Javafx - I cannot get data into a 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.

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.

My full project can be found on GitHub.

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

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

I then create an ObservableList of SNMPInterface objects:

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

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

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

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

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

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?

Answer Source

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