JavaFX中的多布尔绑定

 2023-02-16    313  

问题描述

我试图将复选框绑定到多个复选框,如下所示:

private void bindPanelToPackages(CheckBox panel, CheckBox ...pkg){

    BooleanProperty panelBinding = null;
    BooleanBinding binder = null;

    for(CheckBox p: pkg){
        if(panelBinding == null){
            panelBinding = p.selectedProperty();
        }
        else{
            binder = panelBinding.and(p.selectedProperty());
        }
    }

    if(binder != null){
        panel.selectedProperty().bind(binder);
    }
    else if(panelBinding != null){
        panel.selectedProperty().bindBidirectional(panelBinding);
    }
}

我想要的是,当” pkg”有多个项目时,允许双向组绑定.这样,当我选择软件包时,将自动选择”面板”,或者如果选择”面板”,则将选择/取消选择所有” PKG”.我被困在:

JavaFX中的多布尔绑定

panel.selectedProperty().bind(binder);

并得到

” javafx应用程序线程” java.lang.runtimeException:checkbox.selected:无法设置界值.

由于我对”粘合剂”进行了一个定向绑定.有什么方法可以执行与此相等的事情?

panel.selectedProperty().bindbidirectional(binder);

我似乎在文档中找不到它,或者我没有看正确的地方.谢谢!

推荐答案

条件”选择了所有复选框”,只能表示为BooleanBinding,而不是BooleanProperty.基本上,问题在于,制作该条件false并未明确定义:有很多方法可以做到这一点(即,在未选择的所有复选框中制作所有非空的子集).因此,您不能使用双向绑定:您必须在两个条件中的每个条件上使用听众.

这是一个实现:

// must keep a reference to the Binding to prevent premature
// garbage collection:

BooleanBinding allSelected ;

private void bindPanelToPackages(CheckBox pane, CheckBox... packages) {

    // BooleanBinding that is true if and only if all check boxes in packages are selected:
    allSelected = Bindings.createBooleanBinding(() -> 
        // compute value of binding:
        Stream.of(packages).allMatch(CheckBox::isSelected), 
        // array of thing to observe to recompute binding - this gives the array
        // of all the check boxes' selectedProperty()s.
        Stream.of(packages).map(CheckBox::selectedProperty).toArray(Observable[]::new));

    // update pane's selected property if binding defined above changes
    allSelected.addListener((obs, wereAllSelected, areAllNowSelected) -> 
        pane.setSelected(areAllNowSelected));

    // use an action listener to listen for a direct action on pane, and update all checkboxes
    // in packages if this happens:
    pane.setOnAction(e -> 
        Stream.of(packages).forEach(box -> box.setSelected(pane.isSelected())));

}

和sscce:

import java.util.stream.IntStream;
import java.util.stream.Stream;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MultipleCheckBoxSelection extends Application {

    private BooleanBinding allSelected ;

    @Override
    public void start(Stage primaryStage) {
        CheckBox selectAll = new CheckBox("Select all");
        int numBoxes = 5 ;
        CheckBox[] boxes = IntStream
                .rangeClosed(1,  numBoxes)
                .mapToObj(i -> new CheckBox("Item "+i))
                .toArray(CheckBox[]::new);

        bindPanelToPackages(selectAll, boxes);


        VBox root = new VBox(10, selectAll);
        root.setStyle("-fx-padding: 15;");
        Stream.of(boxes).forEach(box -> box.setStyle("-fx-padding: 0 0 0 10;"));
        Stream.of(boxes).forEach(root.getChildren()::add);
        Scene scene = new Scene(root, 250, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void bindPanelToPackages(CheckBox pane, CheckBox... packages) {

        // BooleanBinding that is true if and only if all check boxes in packages are selected:
        allSelected = Bindings.createBooleanBinding(() -> 
            // compute value of binding:
            Stream.of(packages).allMatch(CheckBox::isSelected), 
            // array of thing to observe to recompute binding - this gives the array
            // of all the check boxes' selectedProperty()s.
            Stream.of(packages).map(CheckBox::selectedProperty).toArray(Observable[]::new));

        // update pane's selected property if binding defined above changes
        allSelected.addListener((obs, wereAllSelected, areAllNowSelected) -> 
            pane.setSelected(areAllNowSelected));

        // use an action listener to listen for a direct action on pane, and update all checkboxes
        // in packages if this happens:
        pane.setOnAction(e -> 
            Stream.of(packages).forEach(box -> box.setSelected(pane.isSelected())));

    }

    public static void main(String[] args) {
        launch(args);
    }
}

以上所述是小编给大家介绍的JavaFX中的多布尔绑定,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33851.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。