I have two TableViews (tableA & tableB) on the same screen (they have the same controller.java)
@FXML TableView <Product> tableA;
@FXML TableView <Product> tableB;
and I want to add the same data in both without repeating :
Product class model
Initialize all tablecolums for both tables (I don't know if this works without initialization)
productName.setCellValueFactory(new PropertyValueFactory<Product, String>("productName"));
price.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
/* ... */
Is there a solution using IDs (tableA, tableB) to access the data???
Do I have to give an ID for each TableColumn?
Controller.java
@FXML TableView <Product> tableA;
@FXML TableView <Product> tableB;
@FXML TableColumn <Product, String> productName;
@FXML TableColumn <Product, Double> price;
@FXML TableColumn <Product, Integer> quantity;
ObservableList<Product> tableData;
private void intializeTableColumns() {
tableData = FXCollections.observableArrayList();
productName.setCellValueFactory(new PropertyValueFactory<Product, String>("productName"));
price.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
quantity.setCellValueFactory(new PropertyValueFactory<Product, Integer>("quantity"));
}
private void insertInTableData() {
Product product = new Product();
/* ...*/
tableData.addAll(product);
/*Add data in both tables */
tableA.setItems(tableData);
tableB.setItems(tableData);
file.fxml
<TableView fx:id="tableA" prefHeight="200.0" styleClass="table-2">
<columns>
<TableColumn maxWidth="-Infinity" minWidth="50.0" prefWidth="50.0" text="N°" />
<TableColumn prefWidth="75.0" text="Product" />
<TableColumn prefWidth="75.0" text="Price" />
<TableColumn prefWidth="75.0" text="Quantity" />
</columns>
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /></columnResizePolicy>
</TableView>
<TableView fx:id="tableB" prefHeight="200.0" styleClass="table-2">
<columns>
<TableColumn maxWidth="-Infinity" minWidth="50.0" prefWidth="50.0" text="N°" />
<TableColumn prefWidth="75.0" text="Product" />
<TableColumn minWidth="-Infinity" prefWidth="75.0" text="Price" />
<TableColumn minWidth="-Infinity" prefWidth="75.0" text="Quantity" />
</columns>
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /></columnResizePolicy>
</TableView>