英文:
How to load the same data in two different TableViews on single screen from the same class model (JavaFX)
问题
我有两个TableView(tableA和tableB)在同一个屏幕上(它们有相同的controller.java)
@FXML TableView<Product> tableA;
@FXML TableView<Product> tableB;
我想在两个表中添加相同的数据而不重复:
Product类模型
为两个表初始化所有tablecolums(我不知道没有初始化是否会起作用)
productName.setCellValueFactory(new PropertyValueFactory<Product, String>("productName"));
price.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
/* ... */
是否可以使用ID(tableA,tableB)来访问数据?是否必须为每个TableColumn分配一个ID?
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 initializeTableColumns() {
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);
/* 在两个表中添加数据 */
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>
希望这对你有所帮助。
英文:
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>
答案1
得分: 4
是的,您可以将相同的数据设置给两个tableViews。在这里,您需要为每个列提供单独的id以设置cellValueFactory。
类似于以下内容:
<TableView fx:id="tableA" prefHeight="200.0" styleClass="table-2">
<columns>
<TableColumn maxWidth="-Infinity" minWidth="50.0" prefWidth="50.0" text="N°" />
<TableColumn fx:id="t1ProductName" prefWidth="75.0" text="Product" />
<TableColumn fx:id="t1Price" prefWidth="75.0" text="Price" />
<TableColumn fx:id="t1Quantity" 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 fx:id="t2ProductName" prefWidth="75.0" text="Product" />
<TableColumn fx:id="t2Price" minWidth="-Infinity" prefWidth="75.0" text="Price" />
<TableColumn fx:id="t2Quantity" minWidth="-Infinity" prefWidth="75.0" text="Quantity" />
</columns>
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /></columnResizePolicy>
</TableView>
控制器代码:
@FXML TableColumn<Product, String> t1ProductName;
@FXML TableColumn<Product, Double> t1Price;
@FXML TableColumn<Product, Integer> t1Quantity;
@FXML TableColumn<Product, String> t2ProductName;
@FXML TableColumn<Product, Double> t2Price;
@FXML TableColumn<Product, Integer> t2Quantity;
ObservableList<Product> tableData;
private void initializeTableColumns() {
tableData = FXCollections.observableArrayList();
t1ProductName.setCellValueFactory(new PropertyValueFactory<Product, String>("productName"));
t1Price.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
t1Quantity.setCellValueFactory(new PropertyValueFactory<Product, Integer>("quantity"));
t2ProductName.setCellValueFactory(new PropertyValueFactory<Product, String>("productName"));
t2Price.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
t2Quantity.setCellValueFactory(new PropertyValueFactory<Product, Integer>("quantity"));
}
英文:
Yes you can set same data to both tableViews. Here, you need to provide separate ids to columns to set cellValueFactory for each columns.
Something like..
<TableView fx:id="tableA" prefHeight="200.0" styleClass="table-2">
<columns>
<TableColumn maxWidth="-Infinity" minWidth="50.0" prefWidth="50.0" text="N°" />
<TableColumn fx:id="t1ProductName" prefWidth="75.0" text="Product" />
<TableColumn fx:id="t1Price" prefWidth="75.0" text="Price" />
<TableColumn fx:id="t1Quantity" 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 fx:id="t2ProductName" prefWidth="75.0" text="Product" />
<TableColumn fx:id="t2Price" minWidth="-Infinity" prefWidth="75.0" text="Price" />
<TableColumn fx:id="t2Quantity" minWidth="-Infinity" prefWidth="75.0" text="Quantity" />
</columns>
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /></columnResizePolicy>
</TableView>
Controller Code:
@FXML TableColumn <Product, String> t1ProductName;
@FXML TableColumn <Product, Double> t1Price;
@FXML TableColumn <Product, Integer> t1Quantity;
@FXML TableColumn <Product, String> t2ProductName;
@FXML TableColumn <Product, Double> t2Price;
@FXML TableColumn <Product, Integer> t2Quantity;
ObservableList<Product> tableData;
private void intializeTableColumns() {
tableData = FXCollections.observableArrayList();
t1ProductName.setCellValueFactory(new PropertyValueFactory<Product, String>("productName"));
t1Price.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
t1Quantity.setCellValueFactory(new PropertyValueFactory<Product, Integer>("quantity"));
t2ProductName.setCellValueFactory(new PropertyValueFactory<Product, String>("productName"));
t2Price.setCellValueFactory(new PropertyValueFactory<Product, Double>("price"));
t2Quantity.setCellValueFactory(new PropertyValueFactory<Product, Integer>("quantity"));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论