Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no <a href="http://en.wikipedia.org/wiki/Logical_conjunction">conjunction</a> api defined in the JavaFX 2.2 platform.</p> <p>You could create a <code>ConjunctionBooleanBinding</code> (aka <code>AllTrueBinding</code>) by subclassing <a href="http://docs.oracle.com/javafx/2/api/javafx/beans/binding/BooleanBinding.html">BooleanBinding</a>. </p> <p>Accept the <a href="http://docs.oracle.com/javafx/2/api/javafx/collections/ObservableList.html">ObservableList</a> in the constructor of your new class, and use the <a href="http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm">low level binding api</a> in an overridden <a href="http://docs.oracle.com/javafx/2/api/javafx/beans/binding/BooleanBinding.html#computeValue%28%29">computeValue</a> method to set the binding value based upon logically anding together all of the boolean values in the list.</p> <p>Here is a sample implementation. The sample could be further performance optimized and make use of WeakReferences, so it does not require manual disposition.</p> <pre><code>import javafx.beans.binding.BooleanBinding; import javafx.beans.property.BooleanProperty; import javafx.collections.*; public class AllTrueBinding extends BooleanBinding { private final ObservableList&lt;BooleanProperty&gt; boundList; private final ListChangeListener&lt;BooleanProperty&gt; BOUND_LIST_CHANGE_LISTENER = new ListChangeListener&lt;BooleanProperty&gt;() { @Override public void onChanged( ListChangeListener.Change&lt;? extends BooleanProperty&gt; change ) { refreshBinding(); } }; private BooleanProperty[] observedProperties = {}; AllTrueBinding(ObservableList&lt;BooleanProperty&gt; booleanList) { booleanList.addListener(BOUND_LIST_CHANGE_LISTENER); boundList = booleanList; refreshBinding(); } @Override protected boolean computeValue() { for (BooleanProperty bp: observedProperties) { if (!bp.get()) { return false; } } return true; } @Override public void dispose() { boundList.removeListener(BOUND_LIST_CHANGE_LISTENER); super.dispose(); } private void refreshBinding() { super.unbind(observedProperties); observedProperties = boundList.toArray(new BooleanProperty[0]); super.bind(observedProperties); this.invalidate(); } } </code></pre> <p>And here is a test harness to demonstrate how it works:</p> <pre><code>import java.util.*; import javafx.beans.property.*; import javafx.collections.FXCollections; import javafx.collections.ObservableList; public class ListBindingTest { final BooleanProperty a = new SimpleBooleanProperty(true); final BooleanProperty b = new SimpleBooleanProperty(true); final BooleanProperty c = new SimpleBooleanProperty(true); final BooleanProperty d = new SimpleBooleanProperty(true); final ObservableList&lt;BooleanProperty&gt; booleanList = FXCollections.observableArrayList(a, b, c, d); public static void main(String[] args) { new ListBindingTest().test(); } private void test() { AllTrueBinding at = new AllTrueBinding(booleanList); System.out.println(at.get() + forArrayString(booleanList)); b.set(false); System.out.println(at.get() + forArrayString(booleanList)); b.set(true); System.out.println(at.get() + forArrayString(booleanList)); booleanList.add(new SimpleBooleanProperty(false)); System.out.println(at.get() + forArrayString(booleanList)); booleanList.remove(3, 5); System.out.println(at.get() + forArrayString(booleanList)); at.dispose(); } private String forArrayString(List list) { return " for " + Arrays.toString(list.toArray()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload