How to load the same data in two different TableViews on single screen from the same class model (JavaFX)

huangapple go评论94阅读模式
英文:

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 &lt;Product&gt; tableA;
@FXML TableView &lt;Product&gt; 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&lt;Product, String&gt;(&quot;productName&quot;));
	
price.setCellValueFactory(new PropertyValueFactory&lt;Product, Double&gt;(&quot;price&quot;));

/* ... */

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 &lt;Product&gt; tableA;
@FXML TableView &lt;Product&gt; tableB;

@FXML TableColumn &lt;Product, String&gt; productName;
@FXML TableColumn &lt;Product, Double&gt; price;
@FXML TableColumn &lt;Product, Integer&gt; quantity;

ObservableList&lt;Product&gt; tableData;

private void intializeTableColumns() {
	
	tableData = FXCollections.observableArrayList();
	
	productName.setCellValueFactory(new PropertyValueFactory&lt;Product, String&gt;(&quot;productName&quot;));
	
	price.setCellValueFactory(new PropertyValueFactory&lt;Product, Double&gt;(&quot;price&quot;));

	quantity.setCellValueFactory(new PropertyValueFactory&lt;Product, Integer&gt;(&quot;quantity&quot;));
	

}
private void insertInTableData() {
	
	Product product = new Product();
	
	/* ...*/
	
	tableData.addAll(product);
   
    /*Add data in both tables */

    tableA.setItems(tableData);
    tableB.setItems(tableData);       

file.fxml

 &lt;TableView fx:id=&quot;tableA&quot; prefHeight=&quot;200.0&quot; styleClass=&quot;table-2&quot;&gt;
         &lt;columns&gt;
             &lt;TableColumn maxWidth=&quot;-Infinity&quot; minWidth=&quot;50.0&quot; prefWidth=&quot;50.0&quot; text=&quot;N&#176;&quot; /&gt;
             &lt;TableColumn prefWidth=&quot;75.0&quot; text=&quot;Product&quot; /&gt;
             &lt;TableColumn prefWidth=&quot;75.0&quot; text=&quot;Price&quot; /&gt;
             &lt;TableColumn prefWidth=&quot;75.0&quot; text=&quot;Quantity&quot; /&gt;
         &lt;/columns&gt;
         &lt;columnResizePolicy&gt;&lt;TableView fx:constant=&quot;CONSTRAINED_RESIZE_POLICY&quot; /&gt;&lt;/columnResizePolicy&gt;
&lt;/TableView&gt;
&lt;TableView fx:id=&quot;tableB&quot; prefHeight=&quot;200.0&quot; styleClass=&quot;table-2&quot;&gt;
         &lt;columns&gt;
             &lt;TableColumn maxWidth=&quot;-Infinity&quot; minWidth=&quot;50.0&quot; prefWidth=&quot;50.0&quot; text=&quot;N&#176;&quot; /&gt;
             &lt;TableColumn prefWidth=&quot;75.0&quot; text=&quot;Product&quot; /&gt;
             &lt;TableColumn minWidth=&quot;-Infinity&quot; prefWidth=&quot;75.0&quot; text=&quot;Price&quot; /&gt;
             &lt;TableColumn minWidth=&quot;-Infinity&quot; prefWidth=&quot;75.0&quot; text=&quot;Quantity&quot; /&gt;
         &lt;/columns&gt;
         &lt;columnResizePolicy&gt;&lt;TableView fx:constant=&quot;CONSTRAINED_RESIZE_POLICY&quot; /&gt;&lt;/columnResizePolicy&gt;
&lt;/TableView&gt;

答案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..

&lt;TableView fx:id=&quot;tableA&quot; prefHeight=&quot;200.0&quot; styleClass=&quot;table-2&quot;&gt;
         &lt;columns&gt;
             &lt;TableColumn maxWidth=&quot;-Infinity&quot; minWidth=&quot;50.0&quot; prefWidth=&quot;50.0&quot; text=&quot;N&#176;&quot; /&gt;
             &lt;TableColumn fx:id=&quot;t1ProductName&quot; prefWidth=&quot;75.0&quot; text=&quot;Product&quot; /&gt;
             &lt;TableColumn fx:id=&quot;t1Price&quot; prefWidth=&quot;75.0&quot; text=&quot;Price&quot; /&gt;
             &lt;TableColumn fx:id=&quot;t1Quantity&quot; prefWidth=&quot;75.0&quot; text=&quot;Quantity&quot; /&gt;
         &lt;/columns&gt;
         &lt;columnResizePolicy&gt;&lt;TableView fx:constant=&quot;CONSTRAINED_RESIZE_POLICY&quot; /&gt;&lt;/columnResizePolicy&gt;
&lt;/TableView&gt;
&lt;TableView fx:id=&quot;tableB&quot; prefHeight=&quot;200.0&quot; styleClass=&quot;table-2&quot;&gt;
         &lt;columns&gt;
             &lt;TableColumn maxWidth=&quot;-Infinity&quot; minWidth=&quot;50.0&quot; prefWidth=&quot;50.0&quot; text=&quot;N&#176;&quot; /&gt;
             &lt;TableColumn fx:id=&quot;t2ProductName&quot; prefWidth=&quot;75.0&quot; text=&quot;Product&quot; /&gt;
             &lt;TableColumn fx:id=&quot;t2Price&quot; minWidth=&quot;-Infinity&quot; prefWidth=&quot;75.0&quot; text=&quot;Price&quot; /&gt;
             &lt;TableColumn fx:id=&quot;t2Quantity&quot; minWidth=&quot;-Infinity&quot; prefWidth=&quot;75.0&quot; text=&quot;Quantity&quot; /&gt;
         &lt;/columns&gt;
         &lt;columnResizePolicy&gt;&lt;TableView fx:constant=&quot;CONSTRAINED_RESIZE_POLICY&quot; /&gt;&lt;/columnResizePolicy&gt;
&lt;/TableView&gt;

Controller Code:

@FXML TableColumn &lt;Product, String&gt; t1ProductName;
@FXML TableColumn &lt;Product, Double&gt; t1Price;
@FXML TableColumn &lt;Product, Integer&gt; t1Quantity;
@FXML TableColumn &lt;Product, String&gt; t2ProductName;
@FXML TableColumn &lt;Product, Double&gt; t2Price;
@FXML TableColumn &lt;Product, Integer&gt; t2Quantity;

ObservableList&lt;Product&gt; tableData;

private void intializeTableColumns() {
    
    tableData = FXCollections.observableArrayList();
    
    t1ProductName.setCellValueFactory(new PropertyValueFactory&lt;Product, String&gt;(&quot;productName&quot;));
    
    t1Price.setCellValueFactory(new PropertyValueFactory&lt;Product, Double&gt;(&quot;price&quot;));

    t1Quantity.setCellValueFactory(new PropertyValueFactory&lt;Product, Integer&gt;(&quot;quantity&quot;));

    t2ProductName.setCellValueFactory(new PropertyValueFactory&lt;Product, String&gt;(&quot;productName&quot;));
    
    t2Price.setCellValueFactory(new PropertyValueFactory&lt;Product, Double&gt;(&quot;price&quot;));

    t2Quantity.setCellValueFactory(new PropertyValueFactory&lt;Product, Integer&gt;(&quot;quantity&quot;));
    

}

huangapple
  • 本文由 发表于 2023年7月27日 21:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定