【问题标题】:Multiple Boolean Binding in JavaFXJavaFX 中的多个布尔绑定
【发布时间】:2015-08-24 22:49:38
【问题描述】:

我正在尝试将一个复选框绑定到多个复选框,如下所示:

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”。我被困在:

panel.selectedProperty().bind(binder);

得到了

“JavaFX 应用程序线程”java.lang.RuntimeException: CheckBox.selected : 无法设置绑定值。

因为我为“binder”做了一个单向绑定。有没有办法我可以执行与此等效的操作?:

panel.selectedProperty().bindBidirectional(binder);

我似乎无法在文档中找到它,或者我没有查看正确的位置。谢谢!

【问题讨论】:

标签: java javafx


【解决方案1】:

条件“所有复选框都被选中”只能表示为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);
    }
}

【讨论】:

  • 感谢您的帮助,但我无法让它工作。选择所有包/项目时,侦听器不会触发以选择窗格。我徒劳地尝试调试 BooleanBinding 以了解使用 NetBeans 发生了什么,并且似乎正确映射了 5 复选框,直到似乎映射到 CheckBox$2 的依赖项与包或依赖项的值不同。我希望这是有道理的......仍在学习......
  • 尝试更新版本;我认为绑定正在收集垃圾。
  • 你是对的。好像是被垃圾回收了。再次感谢!
猜你喜欢
  • 2018-01-29
  • 2015-06-19
  • 1970-01-01
  • 2018-01-17
  • 2013-02-24
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 2011-01-27
相关资源
最近更新 更多