Property Binding

This application demonstrates Pivot's support for "property binding". This feature allows a caller to declaratively create a relationship between a source and target property such that changes to the source are automatically reflected in the target.

Property binding is most commonly used to establish a one-way relationship between a source and target value; however, as shown in the example, bi-directional bindings are also supported. Property relationships are established in markup using the following binding syntax:


"mappingFunction" is an optional script function that can be used to transform a bound source value before it is applied to the target property. If omitted, the source value is simply applied to the target as-is.

The BXML source for the example is shown below:

            <databinding:PropertyBinding title="Property Binding" maximized="true"

                function toUpperCase(value) {
                    return value.toUpperCase();

                function toHex(color) {
                    return PropertyBinding.toHex(color);

                        <Form.Section heading="One-Way Binding">
                            <TextInput bxml:id="textInput" Form.label="Text Input"/>
                            <Label Form.label="Text" text="${textInput.text}"/>
                            <Label Form.label="Uppercase Text" text="${toUpperCase:textInput.text}"/>

                        <Form.Section heading="Two-Way Binding">
                            <TextInput bxml:id="textInput1" Form.label="Text Input 1" text="${textInput2.text}"/>
                            <TextInput bxml:id="textInput2" Form.label="Text Input 2" text="${textInput1.text}"/>

                        <Form.Section heading="Style Binding">
                            <ColorChooserButton bxml:id="colorChooserButton" Form.label="Color Chooser Button"
                            <Label Form.label="Selected Color" text="${colorChooserButton.selectedColor}">
                                <styles color="${colorChooserButton.selectedColor}"/>
                            <Label Form.label="Selected Color (Hex)" text="${toHex:colorChooserButton.selectedColor}">
                                <styles color="${toHex:colorChooserButton.selectedColor}"/>

                        <Form.Section heading="Manual Binding">
                            <ListButton bxml:id="listButton" Form.label="List Button"
                                listData="['Zero', 'One', 'Two', 'Three']" selectedIndex="0"/>
                            <Label bxml:id="listButtonLabel1" Form.label="Selected Item"/>
                            <Label bxml:id="listButtonLabel2" Form.label="Uppercase Selected Item"/>

Note the use of mapping functions to transform the "textInput.text" property to all caps before applying it to the bound label. Similarly, a mapping function is used to transform the "selectedColor" property of the ColorChooserButton to a hex string so it can be used as the value for the "color" style of the selected color label.

Bindings are not limited to BXML - they can also be defined programmatically. For example, the "manual binding" shown in the demo is constructed in Java code as shown in the initialize() method below:

            package org.apache.pivot.tutorials.databinding;

            import java.awt.Color;

            import org.apache.pivot.beans.Bindable;
            import org.apache.pivot.beans.NamespaceBinding;
            import org.apache.pivot.collections.Map;
            import org.apache.pivot.util.Resources;
            import org.apache.pivot.wtk.Window;

            public class PropertyBinding extends Window implements Bindable {
                public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
                    // Bind list button selection to label text
                    NamespaceBinding namespaceBinding1 = new NamespaceBinding(namespace,
                        "listButton.selectedItem", "listButtonLabel1.text");


                    // Bind list button selection to label text with bind mapping
                    NamespaceBinding namespaceBinding2 = new NamespaceBinding(namespace,
                        "listButton.selectedItem", "listButtonLabel2.text", new NamespaceBinding.BindMapping() {
                        public Object evaluate(Object value) {
                            return value.toString().toUpperCase();


                public static String toHex(Color color) {
                    return String.format("#%02X%02X%02X", color.getRed(), color.getBlue(), color.getGreen());

The primary advantage to creating a binding relationship in code is that it can be "unbound" when it is no longer needed; once established, bindings defined in BXML cannot be removed.

Next: Localization