Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将TableView中TableColumn的和绑定到外部标签textProperty()?_Java_Javafx_Binding - Fatal编程技术网

Java 如何将TableView中TableColumn的和绑定到外部标签textProperty()?

Java 如何将TableView中TableColumn的和绑定到外部标签textProperty()?,java,javafx,binding,Java,Javafx,Binding,有一个订单表视图,其中有一列用于表示订单成本。订单对象的成本属性可以在TableView中更改。这意味着Order列单元格可以使用TextFieldTableCell进行更改。此外,TableView之外还有一个标签,它应该表示订单成本的总和。现在,问题是我不知道如何将order costs列的总和绑定到Label的text属性() 下面是一些代码来澄清问题: class Order { private SimpleStringProperty name;

有一个订单表视图,其中有一列用于表示订单成本。订单对象的成本属性可以在TableView中更改。这意味着Order列单元格可以使用TextFieldTableCell进行更改。此外,TableView之外还有一个标签,它应该表示订单成本的总和。现在,问题是我不知道如何将order costs列的总和绑定到Label的text属性()

下面是一些代码来澄清问题:

     class Order {
            private SimpleStringProperty name;
            private SimpleIntegerProperty cost;

            public String getName() {
                return this.name.get();
            }

            public void setName(String name) {
                this.name.set(name);
            }

            public SimpleStringProperty nameProperty() {
                return this.name;
            }

            public Integer getCost() {
                return this.cost.get();
            }

            public void setCost(Integer cost) {
                this.cost.set(cost);
            }

            public SimpleIntegerProperty costProperty() {
                return this.cost;
            }
        }

        TableView<Order> tableView = new TableView<>();

        TableColumn<Order, String> nameColumn = new TableColumn<>();
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());

        TableColumn<Order, String> costColumn = new TableColumn<>();
        costColumn.setCellValueFactory(new PropertyValueFactory<>("cost"));
        costColumn.setCellFactory(TextFieldTableCell.forTableColumn());

        tableView.getColumns().addAll(nameColumn, costColumn);

        Label totalCostLabel = new Label("Total cost should be updated in this label");

        VBox vBox = new VBox();
        vBox.getChildren().addAll(tableView, totalCostLabel);
类顺序{
私有财产名称;
私有财产成本;
公共字符串getName(){
返回此.name.get();
}
公共void集合名(字符串名){
this.name.set(name);
}
公共SimpleStringProperty nameProperty(){
返回此.name;
}
公共整数getCost(){
返回此.cost.get();
}
公共无效设置成本(整数成本){
此.cost.set(成本);
}
公共SimpleIntegerProperty costProperty(){
退还此费用;
}
}
TableView TableView=新建TableView();
TableColumn name column=新建TableColumn();
nameColumn.setCellValueFactory(新属性ValueFactory(“名称”);
nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
TableColumn costColumn=新建TableColumn();
costColumn.setCellValueFactory(新属性ValueFactory(“成本”));
setCellFactory(TextFieldTableCell.forTableColumn());
tableView.getColumns().addAll(nameColumn,costColumn);
Label totalCostLabel=新标签(“应在此标签中更新总成本”);
VBox VBox=新的VBox();
vBox.getChildren().addAll(tableView,totalCostLabel);

您可以执行以下操作:

  • 创建一个映射到
    costProperty()
    ObservableList
    ,并将其用作表的
    项列表:

    tableView.setItems(FXCollections.observableArrayList(
        order -> new Observable[] { order.costProperty() }));
    
    这确保了当列表元素的任何
    costProperty
    发生更改时,列表会触发更新事件(除了在列表中添加或删除元素时触发的常规事件,等等)

  • 创建一个绑定到列表的
    DoubleBinding
    ,并计算总成本:

    DoubleBinding totalCost = Bindings.createDoubleBinding(() -> {
        double total = 0 ;
        for (Order order : tableView.getItems()) {
            total = total + order.getCost();
        }
        return total ;
    }, tableView.getItems());
    
    totalCostLabel.textProperty().bind(totalCost.asString());
    
  • 将标签的
    textProperty()
    绑定到总成本:

    DoubleBinding totalCost = Bindings.createDoubleBinding(() -> {
        double total = 0 ;
        for (Order order : tableView.getItems()) {
            total = total + order.getCost();
        }
        return total ;
    }, tableView.getItems());
    
    totalCostLabel.textProperty().bind(totalCost.asString());
    
  • 如果希望对显示方式进行更多控制,可以向
    asString()
    方法提供

    以下是根据您的代码改编的完整示例:

    import javafx.application.Application;
    import javafx.beans.Observable;
    import javafx.beans.binding.Bindings;
    import javafx.beans.binding.DoubleBinding;
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    import javafx.collections.FXCollections;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.scene.control.cell.TextFieldTableCell;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    import javafx.util.converter.IntegerStringConverter;
    
    
    public class SummingTable extends Application {
    
    
        @Override
        public void start(Stage stage) {
            TableView<Order> tableView = new TableView<>();
    
            tableView.setItems(FXCollections.observableArrayList(
                    order -> new Observable[] { order.costProperty() }));
    
            tableView.getItems().addAll(
                    new Order("Order 1", 10),
                    new Order("Order 2", 20));
    
            tableView.setEditable(true);
    
    
            TableColumn<Order, String> nameColumn = new TableColumn<>();
            nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
            nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
    
            TableColumn<Order, Integer> costColumn = new TableColumn<>();
            costColumn.setCellValueFactory(cellData -> cellData.getValue().costProperty().asObject());
            costColumn.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
    
            tableView.getColumns().addAll(nameColumn, costColumn);
    
            Label totalCostLabel = new Label("Total cost should be updated in this label");
    
            DoubleBinding totalCost = Bindings.createDoubleBinding(() -> {
                double total = 0 ;
                for (Order order : tableView.getItems()) {
                    total = total + order.getCost();
                }
                return total ;
            }, tableView.getItems());
    
            totalCostLabel.textProperty().bind(totalCost.asString());
    
            VBox vBox = new VBox();
            vBox.getChildren().addAll(tableView, totalCostLabel);
    
            Scene scene = new Scene(vBox);
            stage.setScene(scene);
            stage.show();
    
        }
    
        public class Order {
            private final StringProperty name = new SimpleStringProperty();
            private final IntegerProperty cost = new SimpleIntegerProperty();
    
            public Order(String name, int cost) {
                setName(name);
                setCost(cost);
            }
    
            public String getName() {
                return this.name.get();
            }
    
            public void setName(String name) {
                this.name.set(name);
            }
    
            public StringProperty nameProperty() {
                return this.name;
            }
    
            public Integer getCost() {
                return this.cost.get();
            }
    
            public void setCost(Integer cost) {
                this.cost.set(cost);
            }
    
            public IntegerProperty costProperty() {
                return this.cost;
            }
        }
    
        public static void main(String[] args) {
            launch();
        }
    
    }
    
    导入javafx.application.application;
    导入javafx.beans.Observable;
    导入javafx.beans.binding.Bindings;
    导入javafx.beans.binding.DoubleBinding;
    导入javafx.beans.property.IntegerProperty;
    导入javafx.beans.property.SimpleIntegerProperty;
    导入javafx.beans.property.SimpleStringProperty;
    导入javafx.beans.property.StringProperty;
    导入javafx.collections.FXCollections;
    导入javafx.scene.scene;
    导入javafx.scene.control.Label;
    导入javafx.scene.control.TableColumn;
    导入javafx.scene.control.TableView;
    导入javafx.scene.control.cell.PropertyValueFactory;
    导入javafx.scene.control.cell.TextFieldTableCell;
    导入javafx.scene.layout.VBox;
    导入javafx.stage.stage;
    导入javafx.util.converter.IntegerStringConverter;
    公共类SummingTable扩展了应用程序{
    @凌驾
    公众假期开始(阶段){
    TableView TableView=新建TableView();
    tableView.setItems(FXCollections.observableArrayList(
    order->new Observable[]{order.costProperty()});
    tableView.getItems().addAll(
    新命令(“命令1”,10),
    新命令(“命令2”,20));
    tableView.setEditable(true);
    TableColumn name column=新建TableColumn();
    nameColumn.setCellValueFactory(新属性ValueFactory(“名称”);
    nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
    TableColumn costColumn=新建TableColumn();
    costColumn.setCellValueFactory(cellData->cellData.getValue().costProperty().asObject());
    setCellFactory(TextFieldTableCell.forTableColumn(新的IntegerStringConverter());
    tableView.getColumns().addAll(nameColumn,costColumn);
    Label totalCostLabel=新标签(“应在此标签中更新总成本”);
    DoubleBinding totalCost=Bindings.createDoubleBinding(()->{
    双倍合计=0;
    对于(订单:tableView.getItems()){
    总计=总计+订单。getCost();
    }
    返回总数;
    },tableView.getItems());
    totalCostLabel.textProperty().bind(totalCost.asString());
    VBox VBox=新的VBox();
    vBox.getChildren().addAll(tableView,totalCostLabel);
    场景=新场景(vBox);
    舞台场景;
    stage.show();
    }
    公共阶级秩序{
    私有最终StringProperty名称=新SimpleStringProperty();
    私有最终IntegerProperty成本=新SimpleIntegerProperty();
    公共秩序(字符串名称,整数成本){
    集合名(名称);
    设定成本(成本);
    }
    公共字符串getName(){
    返回此.name.get();
    }
    公共void集合名(字符串名){
    this.name.set(name);
    }
    公共字符串属性nameProperty(){
    返回此.name;
    }
    公共整数getCost(){
    返回此.cost.get();
    }
    公共无效设置成本(整数成本){
    此.cost.set(成本);
    }
    公共整数属性costProperty(){
    退还此费用;
    }
    }
    公共静态void main(字符串[]args){
    发射();