I have a Model that accepts a row number, column, and its value. I want to populate a table view where the value of the column is pointing to the value in the Model on the current column. (Not a native English speaker, so sorry).
Here is what I have so far
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);
}
}
}
Expected Output
+-----+--------+--------+--------+
| 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 |
+-----+--------+--------+--------+
I have seen this example https://stackoverflow.com/a/27497094/14660358, but I could hardly follow and implement it to my problem.
The only solution I have so far was to reconstruct my model to something like this :
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);
}
}
Then populate the table's column value property to
colCol1.setCellValueFactory(new PropertyValueFactory<>("column1"));
But I knew this is not a good programming practice. So I was hoping for someone to give atleast a much more detailed example if possible :)
PS: I'm still working on the question's title, so sorry if this misleads someone.