如何在 JavaFx TableView 中添加 TableView 页脚

How to add TableView footer in JavaFx TableView(如何在 JavaFx TableView 中添加 TableView 页脚)

本文介绍了如何在 JavaFx TableView 中添加 TableView 页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在 JavaFX TableView 中添加 table footercolumn footer.我正在寻找添加一个 TableView ,它将在 TableView 页脚处显示购买的商品数量和销售价格,以及商品总数和总和.我查看了各种资源,但找不到与 TableView 关联的页脚属性.知道怎么做吗?

模型类

 包 javafxapplication8;公共类TestModel {私有字符串 itemName = null;私有 int pricePerUnit = 0;私人双倍数量 = 0.0;私人双倍金额 = 0.0;公共测试模型(){}公共测试模型(字符串 argitemName,int argpricePerUnit,双 argquantity,双 argamount){项目名称 = 参数名称;pricePerUnit = argpricePerUnit;数量 = 参数数量;金额= argamount;}公共无效 setItemName(字符串 argitemName){项目名称 = 参数名称;}公共无效 setPricePerUnit(int argpricePerUnit) {pricePerUnit = argpricePerUnit;}公共无效setQuantity(双参数){数量 = 参数数量;}公共无效setAmount(双argamount){金额= argamount;}公共字符串 getItemName() {返回项目名称;}公共 int getPricePerUnit() {返回单价;}公共双getQuantity(){退货数量;}公共双getAmount(){退货金额;}@覆盖公共字符串 toString() {返回 this.itemName + "+ this.pricePerUnit + ""+ this.quantity + ""+ 这个数量;}}

XML 代码

控制器类

package javafxapplication8;导入 java.net.URL;导入 java.util.ResourceBundle;导入 javafx.collections.FXCollections;导入 javafx.collections.ObservableList;导入 javafx.fxml.FXML;导入 javafx.fxml.Initializable;导入 javafx.scene.control.TableColumn;导入 javafx.scene.control.TableView;导入 javafx.scene.control.cell.PropertyValueFactory;导入 javafx.scene.layout.AnchorPane;公共类 TVCTestModel 实现 Initializable {@FXML私有表列<TestModel, String>项目名称;@FXML私有表列<TestModel, Integer>每单位价格;@FXML私有表列<TestModel, Double>数量;@FXML私有表列<TestModel, Double>数量;@FXML私有表视图<TestModel>我的表视图;public ObservableList<TestModel>objList = FXCollections.observableArrayList();@FXML私人 AnchorPane 锚;私有静态TestModel curTestModel;@覆盖公共无效初始化(URL url,ResourceBundle rb){this.itemName.setCellValueFactory(new PropertyValueFactory<>(itemName"));this.pricePerUnit.setCellValueFactory(new PropertyValueFactory<>(pricePerUnit"));this.quantity.setCellValueFactory(new PropertyValueFactory<>(quantity"));this.amount.setCellValueFactory(new PropertyValueFactory<>(amount"));objList.add(new TestModel("Item 1", 10, 4, 400));objList.add(new TestModel("Item 2", 20, 5, 1000));objList.add(new TestModel("Item 3", 30, 6, 1800));objList.add(new TestModel("Item 4", 400, 7, 2800));System.out.println(objList.size());myTableView.setItems(objList);}}

MainMethod 类

 包 javafxapplication8;导入 java.io.IOException;导入 java.util.logging.Level;导入 java.util.logging.Logger;导入 javafx.application.Application;导入 javafx.fxml.FXMLLoader;导入 javafx.scene.Parent;导入javafx.scene.Scene;导入javafx.stage.Stage;公共类 JavaFXApplication8 扩展应用程序 {@覆盖公共无效开始(阶段主要阶段){尝试 {父根 = FXMLLoader.load(getClass().getResource("TVCTestModel.fxml"));场景场景 = 新场景(根);primaryStage.setScene(场景);主舞台.show();} 捕捉(IOException ex){Logger.getLogger(JavaFXApplication8.class.getName()).log(Level.SEVERE, null, ex);}}公共静态无效主要(字符串[]参数){启动(参数);}}

解决方案

您更新的问题已重新打开,并且提供的图像表明,而不是

TVCTestModel.fxml:

<代码>…<底部><HBox 对齐 =CENTER_RIGHT";style="-fx-spacing: 5px;"><孩子们><标签fx:id=labelQ"/><标签fx:id=标签A"/></孩子们></HBox></底部>…

TVCTestModel.java

@FXML 私有标签 labelQ;@FXML 私有标签 labelA;@覆盖公共无效初始化(URL url,ResourceBundle rb){…双总和数量 = 0;双总和 = 0;对于(TestModel o:objList){sumQuantity += o.getQuantity();sumAmout += o.getAmount();}labelQ.setText("数量:";+ NumberFormat.getNumberInstance().format(sumQuantity));labelA.setText("金额:"+ NumberFormat.getCurrencyInstance().format(sumAmout));}

如果您稍后决定让您的表格可编辑,如图所示 这里,你应该考虑这些修改:

  • 迁移到模型类中的可观察属性,如此处所示.

  • 使用 extractor,如图这里 和 这里;您的 extractor 将包含数量和金额的属性;然后,您的控制器可以更新 ListChangeListener 中的汇总字段.

I am stuck on how to add table footer or column footer in JavaFX TableView. I am looking to add a TableView which will show purchased items with quantities and sells price in columns and total items count and total sum at the TableView footer. I looked at various resources, but could not find footer property associated with TableView. Any idea how to do it?

Model Class

    package javafxapplication8;
    
    public class TestModel {
    
        private String itemName = null;
    
        private int pricePerUnit = 0;
    
        private double quantity = 0.0;
    
        private double amount = 0.0;
    
        public TestModel() {
        }
    
        public TestModel(String argitemName, int argpricePerUnit, double argquantity, double argamount) {
            itemName = argitemName;
            pricePerUnit = argpricePerUnit;
            quantity = argquantity;
            amount = argamount;
    
        }
    
        public void setItemName(String argitemName) {
            itemName = argitemName;
        }
    
        public void setPricePerUnit(int argpricePerUnit) {
            pricePerUnit = argpricePerUnit;
        }
    
        public void setQuantity(double argquantity) {
            quantity = argquantity;
        }
    
        public void setAmount(double argamount) {
            amount = argamount;
        }
    
        public String getItemName() {
            return itemName;
        }
    
        public int getPricePerUnit() {
            return pricePerUnit;
        }
    
        public double getQuantity() {
            return quantity;
        }
    
        public double getAmount() {
            return amount;
        }
    
        @Override
        public String toString() {
            return this.itemName + "" + this.pricePerUnit + "" + this.quantity + "" + this.amount;
        }
    
    }

XML Code

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" fx:id="anchor" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication8.TVCTestModel">
    <children>
        <VBox prefHeight="564.0" prefWidth="857.0">
            <children>
                <HBox alignment="BOTTOM_LEFT" prefHeight="100.0" prefWidth="1613.0" spacing="20.0" />            
                <BorderPane prefHeight="695.0" prefWidth="1618.0">
                    <center>
                        <VBox prefHeight="544.0" prefWidth="772.0">
                            <children>
                                <HBox prefHeight="65.0" prefWidth="1618.0" />
                                <HBox prefHeight="426.0" prefWidth="857.0">
                                    <children>
                                        <HBox alignment="CENTER" prefHeight="225.0" prefWidth="857.0">
                                            <children>                  
                                                <TableView fx:id="myTableView" prefHeight="419.0" prefWidth="816.0">
                                                    <columns>
                                                        <TableColumn fx:id="itemName" prefWidth="200.0" text="Item Name" />
                                                        <TableColumn fx:id="pricePerUnit" prefWidth="200.0" text="Price Per Unit" />
                                                        <TableColumn fx:id="quantity" prefWidth="200.0" text="Quantity" />
                                                        <TableColumn fx:id="amount" prefWidth="200.0" text="Amount" />
                                                    </columns>
                                                </TableView>
                                            </children>
                                        </HBox>
                                    </children>
                                </HBox>
                            </children>
                        </VBox>              
                    </center>
                    <bottom>

                    </bottom>
                </BorderPane>         
            </children>
        </VBox>
    </children>
</AnchorPane>

Controller Class

package javafxapplication8;

import java.net.URL;

import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;

import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;

public class TVCTestModel implements Initializable {

    @FXML
    private TableColumn<TestModel, String> itemName;

    @FXML
    private TableColumn<TestModel, Integer> pricePerUnit;

    @FXML
    private TableColumn<TestModel, Double> quantity;

    @FXML
    private TableColumn<TestModel, Double> amount;
    @FXML
    private TableView<TestModel> myTableView;
    public ObservableList<TestModel> objList = FXCollections.observableArrayList();
    @FXML
    private AnchorPane anchor;
    private static TestModel curTestModel;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        this.itemName.setCellValueFactory(new PropertyValueFactory<>("itemName"));
        this.pricePerUnit.setCellValueFactory(new PropertyValueFactory<>("pricePerUnit"));
        this.quantity.setCellValueFactory(new PropertyValueFactory<>("quantity"));
        this.amount.setCellValueFactory(new PropertyValueFactory<>("amount"));

        objList.add(new TestModel("Item 1", 10, 4, 400));
        objList.add(new TestModel("Item 2", 20, 5, 1000));
        objList.add(new TestModel("Item 3", 30, 6, 1800));
        objList.add(new TestModel("Item 4", 400, 7, 2800));
        System.out.println(objList.size());
        myTableView.setItems(objList);
    }

}

MainMethod Class

   package javafxapplication8;
    
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class JavaFXApplication8 extends Application {
    
        @Override
        public void start(Stage primaryStage) {
    
            try {
                Parent root = FXMLLoader.load(getClass().getResource("TVCTestModel.fxml"));
                Scene scene = new Scene(root);
                primaryStage.setScene(scene);
                primaryStage.show();
    
            } catch (IOException ex) {
                Logger.getLogger(JavaFXApplication8.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }

解决方案

Your updated question was reopened, and the image provided suggests that, instead of a footer, you want two summary fields. As your table is not editable, a simple approximation is illustrated here—add two labels to the view, and iterate the table's model in the controller to establish the localized result:

TVCTestModel.fxml:

…
<bottom>
    <HBox alignment="CENTER_RIGHT" style="-fx-spacing: 5px;">
        <children>
            <Label fx:id="labelQ"/>
            <Label fx:id="labelA"/>
        </children>
    </HBox>
</bottom>
…

TVCTestModel.java

@FXML private Label labelQ;
@FXML private Label labelA;

@Override
public void initialize(URL url, ResourceBundle rb) {
    …
    double sumQuantity = 0;
    double sumAmout = 0;
    for (TestModel o : objList) {
        sumQuantity += o.getQuantity();
        sumAmout += o.getAmount();
    }
    labelQ.setText("Quantity: "
        + NumberFormat.getNumberInstance().format(sumQuantity));
    labelA.setText("Amount: "
       + NumberFormat.getCurrencyInstance().format(sumAmout));
}

If you later decide to make your table editable, as shown here, you should consider these modifications:

  • Migrate to observable properties in your model class, as shown here.

  • Create your ObservableList model with an extractor, as shown here and here; your extractor would include properties for quantity and amount; your controller could then update the summary field in a ListChangeListener.

这篇关于如何在 JavaFx TableView 中添加 TableView 页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 JavaFx TableView 中添加 TableView 页脚

基础教程推荐