首页 > 解决方案 > 如何根据特定的行号和列号设置 TableView 的值

问题描述

我有一个接受行号、列及其值的模型。我想填充一个表视图,其中列的值指向当前列上模型中的值。(不是以英语为母语的人,很抱歉)。

这是我到目前为止所拥有的

public class DynamicTableView extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        ObservableList<TableData> observableList= FXCollections.observableArrayList(
                new TableData(1, 1, 123.00),
                new TableData(1, 2, 124.00),
                new TableData(1, 3, 125.00),
                new TableData(2, 1, 126.00),
                new TableData(2, 2, 127.00),
                new TableData(2, 3, 128.00),
                new TableData(3, 1, 129.00),
                new TableData(3, 2, 130.00),
                new TableData(3, 3, 131.19)
        );

        TableView<TableData> tableView = new TableView<>(observableList);

        //Creating columns
        TableColumn<TableData, Short> colRow = new TableColumn<>("Row");
        TableColumn<TableData, Double> colCol1 = new TableColumn<>("Column 1");
        TableColumn<TableData, Double> colCol2 = new TableColumn<>("Column 2");
        TableColumn<TableData, Double> colCol3 = new TableColumn<>("Column 3");

        //Set the value
        colRow.setCellValueFactory(new PropertyValueFactory<>("rowNumber"));
     /* colCol1.setCellValueFactory(new PropertyValueFactory<>("value"));
        colCol2.setCellValueFactory(new PropertyValueFactory<>("value"));
        colCol3.setCellValueFactory(new PropertyValueFactory<>("value")); */

        //add columns to table
        tableView.getColumns().addAll(colRow, colCol1, colCol2, colCol3);

        JFXButton button = new JFXButton("Add Data");
        button.setOnAction (e-> {
            observableList.add(new TableData(5, 1, 132.20));
        });

        VBox root = new VBox(10, button, tableView);
        root.setAlignment(Pos.CENTER);
        primaryStage.setScene(new Scene(root, 500, 300));
        primaryStage.show();

    }

    public class TableData {
        SimpleIntegerProperty rowNumber;
        SimpleIntegerProperty columnNumber;
        SimpleDoubleProperty value;

        public TableData(int row, int col, double val) {
            this.rowNumber = new SimpleIntegerProperty(row);
            this.columnNumber = new SimpleIntegerProperty(col);
            this.value = new SimpleDoubleProperty(val);
        }
    }
}

预期产出

 +-----+--------+--------+--------+
 | Row | Col 1  | Col 2  | Col 3  |
 +-----+--------+--------+--------+
 | 1   | 123.00 | 124.00 | 125.00 |
 +-----+--------+--------+--------+
 | 2   | 126.00 | 127.00 | 128.00 |
 +-----+--------+--------+--------+
 | 3   | 129.00 | 130.00 | 131.19 |
 +-----+--------+--------+--------+

我已经看过这个例子https://stackoverflow.com/a/27497094/14660358,但我几乎无法遵循并实施它来解决我的问题。

到目前为止,我唯一的解决方案是将我的模型重建为如下所示:

public class TableData {
        SimpleIntegerProperty rowNumber;
        SimpleDoubleProperty column1;
        SimpleDoubleProperty column2;
        SimpleDoubleProperty column3;
        
        public TableData(int row, double col1, double col2, double col3) {
            this.rowNumber = new SimpleIntegerProperty(row);
            this.column1 = new SimpleDoubleProperty(col1);
            this.column2 = new SimpleDoubleProperty(col2);
            this.column3 = new SimpleDoubleProperty(col3);
        }
    }

然后将表的列值属性填充为

colCol1.setCellValueFactory(new PropertyValueFactory<>("column1"));

但我知道这不是一个好的编程习惯。所以我希望有人在可能的情况下至少给出一个更详细的例子:)

PS:我仍在研究问题的标题,如果这会误导某人,我深表歉意。

标签: javafxtableview

解决方案


推荐阅读