javafx tableview data from List

后端 未结 2 608
情话喂你
情话喂你 2020-12-21 11:59

I\'ve got problem with TableView in JavaFX.

Creating tables from existing class with all fields defined is easy.

But I\'m wondering if it is possible to make

2条回答
  •  失恋的感觉
    2020-12-21 12:45

    You can do this by replacing String to StringProperty in the List:

    @FXML
    private TableView> testTable;
    

    then:

    TableColumn, String> coll = new TableColumn<>("one");
    

    add the cellValueFactories:

    col1.setCellValueFactory(data -> data.getValue().get(0));
    col2.setCellValueFactory(data -> data.getValue().get(1));
    .
    .
    

    and so on.

    This means the first element of the list will be used in col1, the second element of the list will be used in col2.

    Then you can populate the list like:

    ObservableList> data = FXCollections.observableArrayList();
    List firstRow = new ArrayList<>();
    firstRow.add(0, new SimpleStringProperty("Andrew"));
    firstRow.add(1, new SimpleStringProperty("Smith"));
    .
    .
    .
    data.add(firstRow);
    .
    .
    .
    and so on...
    table.setItems(data);
    

    It is doable this way but I would say it is a very bad practice.

提交回复
热议问题