How to set value to cell in TableView in javafx


Question

I am newbie on javafx. I already code in core java to set value to table. But now i am converting my project in javafx to maintain code and to improve design.

I am trying to set value to table view but i am not getting how to set value to tableview.

Here is my code which i am using for swing.

String testbedName = treePath.getPathComponent(1).toString();
    TestBed currentTestbed = getTbF().get(testbedName);
    propertyTable.getModel().setValueAt(currentTestbed.getTestBedName(), 0, 1);
    propertyTable.getModel().setValueAt(currentTestbed.getTestBedFilePath() + "\\" + testbedName + ".xml", 1, 1);
    propertyTable.getModel().setValueAt(currentTestbed.getTbElements().size(), 2, 1);  

But i need to convert this code in javafx means from JTable to TableView. but tableview not find getModel() method.

I search but didn't get how to set value to table view.
Please give me reference or hint.

Thanks in advance.

1
1
12/12/2013 3:31:34 PM

In JavaFX you generally don't set the values cell by cell like that. In your case, for example, you would

  • place your TestBeds in the table:

    TableView<TestBed> table = new TableView<>();
    table.getItems().addAll(yourTestBeds);
    
  • you then create the columns you need, by providing appropriate cell value factories, for example (using Java 8 syntax):

    TableColumn<TestBed, String> name = new TableColumn<>();
    name.setCellValueFactory(c-> new SimpleStringProperty(c.getValue().getTestBedName()));
    

    If you use Java 7:

    name.setCellValueFactory(new Callback<CellDataFeatures<TestBed, String>, ObservableValue<String>>() {
        @Override public ObservableValue<String> call(CellDataFeatures<TestBed, String> c) {
            return new SimpleStringProperty(c.getValue().getTestBedName()));
        }
    });
    
  • Finally you add the columns to the table:

    table.getColumns().addAll(name, someOtherColumn);
    
6
12/12/2013 4:08:12 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon