SlideShare a Scribd company logo
Oracle® JavaFX
Using JavaFX Properties and Binding
Release 2.1
E20469-02




April 2012
Oracle JavaFX/Using JavaFX Properties and Binding, Release 2.1

E20469-02

Copyright © 2011,2012 Oracle and/or its affiliates. All rights reserved.

Primary Author:     Scott Hommel

Contributing Author:

Contributor:

This software and related documentation are provided under a license agreement containing restrictions on
use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your
license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license,
transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse
engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is
prohibited.
The information contained herein is subject to change without notice and is not warranted to be error-free. If
you find any errors, please report them to us in writing.

If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it
on behalf of the U.S. Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data
delivered to U.S. Government customers are "commercial computer software" or "commercial technical data"
pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As
such, the use, duplication, disclosure, modification, and adaptation shall be subject to the restrictions and
license terms set forth in the applicable Government contract, and, to the extent applicable by the terms of
the Government contract, the additional rights set forth in FAR 52.227-19, Commercial Computer Software
License (December 2007). Oracle America, Inc., 500 Oracle Parkway, Redwood City, CA 94065.

This software or hardware is developed for general use in a variety of information management
applications. It is not developed or intended for use in any inherently dangerous applications, including
applications that may create a risk of personal injury. If you use this software or hardware in dangerous
applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other
measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages
caused by use of this software or hardware in dangerous applications.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.

Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks
are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD,
Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced
Micro Devices. UNIX is a registered trademark licensed through X/Open Company, Ltd.

This software or hardware and documentation may provide access to or information on content, products,
and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly
disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle
Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your
access to or use of third-party content, products, or services.
Contents

1 Using JavaFX Properties and Binding
   Overview....................................................................................................................................................   1-1
   Understanding Properties.......................................................................................................................                1-1
   Using the High-Level Binding API.......................................................................................................                        1-4
   Exploring Observable, ObservableValue, InvalidationListener, and ChangeListener ..............                                                                 1-6
   Using the Low-Level Binding API ........................................................................................................                       1-7




                                                                                                                                                                   iii
iv
1
                                         Using JavaFX Properties
                                         1


                                                    and Binding

           This tutorial describes properties and binding in JavaFX 2.0, using working examples
           that you can compile and run.


Overview
           For many years, the Java programming language has used the JavaBeans component
           architecture to represent the property of an object. This model consists of both an API
           and a design pattern; it is widely understood by Java application developers and
           development tools alike. This release introduces property support into JavaFX,
           support that is based on the proven JavaBeans model, but expanded and improved.
           JavaFX properties are often used in conjunction with binding, a powerful mechanism
           for expressing direct relationships between variables. When objects participate in
           bindings, changes made to one object will automatically be reflected in another object.
           This can be useful in a variety of applications. For example, binding could be used in a
           bill invoice tracking program, where the total of all bills would automatically be
           updated whenever an individual bill is changed. Or, binding could be used in a
           graphical user interface (GUI) that automatically keeps its display synchronized with
           the application's underlying data.
           Bindings are assembled from one or more sources, known as dependencies. A binding
           observes its list of dependencies for changes, and then updates itself automatically
           after a change has been detected.
           The binding APIs are divided into two broad categories:
           1.   The High-Level API: Provides a simple way to create bindings for the most
                common use cases. Its syntax is easy to learn and use, especially in environments
                that provide code completion, such as the NetBeans IDE.
           2.   The Low-Level API: Provides additional flexibility, and can be used by advanced
                developers in situations where the High-Level API is insufficient. The Low-Level
                API was designed for fast execution and small
                memory footprint.
           The remainder of this tutorial describes these APIs, and provides working code
           examples that you can compile and run.


Understanding Properties
           As mentioned in the overview, JavaFX property support is based on the well-known
           property model established by the JavaBeans component architecture. This section


                                                            Using JavaFX Properties and Binding 1-1
Understanding Properties


                    provides a brief overview of what that means, then explains how properties apply to
                    JavaFX.
                    The Java programming language makes it possible to encapsulate data within an
                    object, but it does not enforce any specific naming conventions for the methods that
                    you define. For example, your code might define a Person class, which encapsulates a
                    first name and a last name. But without naming conventions, different programmers
                    might choose different names for these methods: read_first(), firstName(),
                    getFN(), etc. would all be perfectly valid choices. However, there is no guarantee that
                    these names will be meaningful to other developers.
                    The JavaBeans component architecture addressed this problem by defining some
                    simple naming conventions that bring consistency across projects. In JavaBeans
                    programming, the full signatures for these methods would be: public void
                    setFirstName(String name), public String getFirstName(), public
                    void setLastName(String name), and public String getLastName().
                    This naming pattern is easily recognizable, both to human programmers and to editing
                    tools, such as the NetBeans IDE. In JavaBeans terminology, the Person object is said
                    to contain firstName and lastName properties.
                    The JavaBeans model also provides support for complex property types, plus an event
                    delivery system. It also contains a number of support classes, all available as an API
                    under the java.beans package. Therefore, mastering JavaBeans programming
                    involves learning the required naming conventions and its corresponding API. (For
                    more background reading on JavaBeans in general, see the JavaBeans lesson of the
                    Java Tutorial at
                    http://download.oracle.com/javase/tutorial/javabeans).
                    Similarly, understanding JavaFX properties also requires learning a few new APIs and
                    naming conventions. In JavaFX, it is entirely possible that you will only be interested
                    in using classes that contain properties (as opposed to implementing properties in your
                    own custom classes), but Example 1 will familiarize you with the new method naming
                    conventions that form the JavaFX property pattern. It defines a class named Bill,
                    which implements a single property named amountDue.

                    Example 1–1 Defining a Property
                    package propertydemo;

                    import javafx.beans.property.DoubleProperty;
                    import
                    javafx.beans.property.SimpleDoubleProperty;

                    class Bill {

                        // Define a variable to store the property
                        private DoubleProperty amountDue = new
                    SimpleDoubleProperty();

                           // Define a getter for the property's value
                           public final double getAmountDue(){return amountDue.get();}

                           // Define a setter for the property's value
                           public final void setAmountDue(double value){amountDue.set(value);}

                            // Define a getter for the property itself
                           public DoubleProperty amountDueProperty() {return amountDue;}

                    }



1-2 Oracle JavaFX/Using JavaFX Properties and Binding
Understanding Properties


The amountDue object — an instance of the
javafx.beans.property.DoubleProperty class — is marked as private to
encapsulate it from the outside world. This is standard practice in both Java and
JavaBeans application development. Note however that the object’s type is not one of
the standard Java primitives, but rather, a new wrapper class that encapsulates a Java
primitive and adds some extra functionality (the classes under
javafx.beans.property all contain built-in support for observability and binding
as part of their design).
The property method naming conventions are as follows:
■   The getAmountDue() method is a standard getter that returns the current value
    of the amountDue property. By convention, this method is declared as final.
    Note that the return type for this method is double, not DoubleProperty.
■   The setAmountDue(double) method (also final) is a standard setter that
    allows a caller to set the property's value. The setter method is optional. Its
    parameter is also of type double.
■   Finally, the amountDueProperty() method defines the property getter. This is a
    new convention in which the method name contains the name of the property
    (amountDue, in this case), followed by the word "Property." The return type is the
    same as the property itself (DoubleProperty, in this example).
When building GUI applications with JavaFX, you will notice that certain classes in
the API already implement properties. For example, the
javafx.scene.shape.Rectangle class contains properties for arcHeight,
arcWidth, height, width, x, and y. For each of these properties there will be
corresponding methods that match the conventions previously described. For
example, getArcHeight(), setArcHeight(double), arcHeightProperty(),
which together indicate (to both developers and tools) that the given property exists.
You can also add a change listener to be notified when the property's value has
changed, as shown in Example 2.

Example 1–2 Using a ChangeListener
package propertydemo;

import javafx.beans.value.ObservableValue;
import javafx.beans.value.ChangeListener;

public class Main {

    public static void main(String[] args) {

        Bill electricBill = new Bill();

         electricBill.amountDueProperty().addListener(new ChangeListener(){
          @Override public void changed(ObservableValue o,Object oldVal,
                   Object newVal){
               System.out.println("Electric bill has changed!");
          }
        });

        electricBill.setAmountDue(100.00);

    }
}




                                                 Using JavaFX Properties and Binding 1-3
Using the High-Level Binding API


                    Running this example will print the message "Electric bill has changed" to standard
                    output, proving that the change listener notification
                    is working.


Using the High-Level Binding API
                    The High-Level API is the quickest and easiest way to begin using bindings in your
                    own applications. It consists of two parts: the Fluent API, and the Bindings class. The
                    Fluent API exposes methods on the various dependency objects, whereas the
                    Bindings class provides static factory methods instead.
                    To begin using the Fluent API, consider a simple use case in which two integers are
                    bound so that their values are always added together. In Example 3, there are three
                    variables involved: num1 (a dependency), num2 (a dependency), and sum (the
                    binding). The dependency types are both IntegerProperty, and the binding itself is
                    NumberBinding.

                    Example 1–3 Using the Fluent API
                    package bindingdemo;

                    import javafx.beans.property.IntegerProperty;
                    import
                    javafx.beans.property.SimpleIntegerProperty;
                    import javafx.beans.binding.NumberBinding;

                    public class Main {

                        public static void main(String[] args) {
                            IntegerProperty num1 = new
                    SimpleIntegerProperty(1);
                            IntegerProperty num2 = new
                    SimpleIntegerProperty(2);
                            NumberBinding sum = num1.add(num2);
                            System.out.println(sum.getValue());
                            num1.set(2);
                            System.out.println(sum.getValue());
                        }
                    }

                    This code binds the two dependencies, prints their sum, then changes the value of
                    num1 and prints the sum again. The results are "3" and "4", which proves that the
                    binding is working.
                    You could also use the Bindings class to do the same thing, as shown in Example 4.

                    Example 1–4 Using the Bindings Class
                    package bindingdemo;

                    import javafx.beans.property.IntegerProperty;
                    import
                    javafx.beans.property.SimpleIntegerProperty;
                    import javafx.beans.binding.NumberBinding;
                    import javafx.beans.binding.Bindings;

                    public class Main {

                         public static void main(String[] args) {



1-4 Oracle JavaFX/Using JavaFX Properties and Binding
Using the High-Level Binding API


       IntegerProperty num1 = new
SimpleIntegerProperty(1);
       IntegerProperty num2 = new
SimpleIntegerProperty(2);
       NumberBinding sum = Bindings.add(num1,num2);
       System.out.println(sum.getValue());
       num1.setValue(2);
       System.err.println(sum.getValue());
    }
}

Example 5 combines the two approaches:

Example 1–5 Combining Both Approaches
package bindingdemo;

import javafx.beans.property.IntegerProperty;
import
javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.binding.NumberBinding;
import javafx.beans.binding.Bindings;

public class Main {

    public static void main(String[] args) {
       IntegerProperty num1 = new
SimpleIntegerProperty(1);
       IntegerProperty num2 = new
SimpleIntegerProperty(2);
       IntegerProperty num3 = new
SimpleIntegerProperty(3);
       IntegerProperty num4 = new
SimpleIntegerProperty(4);
       NumberBinding total =
         Bindings.add(num1.multiply(num2),num3.multiply(num4));
       System.out.println(total.getValue());
       num1.setValue(2);
       System.err.println(total.getValue());
    }
}

Example 5 modifies the code to invoke the multiply method from the Fluent API,
and add from the Bindings class. You should also know that the High-Level API lets
you mix types when defining arithmetic operations. The type of the result is defined
by the same rules as the Java programming language:
1.   If one of the operands is a double, the result is a double.
2.   If not and one of the operands is a float, the result is a float.
3.   If not and one of the operands is a long, the result is a long.
4.   The result is an integer otherwise.
The next section explores observability, and demonstrates how invalidation listeners
differ from change listeners.




                                                  Using JavaFX Properties and Binding 1-5
Exploring Observable, ObservableValue, InvalidationListener, and ChangeListener



Exploring Observable, ObservableValue,
InvalidationListener, and ChangeListener
                    The binding API defines a set of interfaces that enable objects to be notified when a
                    value change or invalidation takes place. The Observable and ObservableValue
                    interfaces fire the change notifications, and the InvalidationListener and
                    ChangeListener interfaces receive them. The difference between the two is that
                    ObservableValue wraps a value and fires its changes to any registered
                    ChangeListener, whereas Observable (which does not wrap a value) fires its
                    changes to any registered InvalidationListener.
                    The JavaFX binding and property implementations all support lazy evaluation, which
                    means that when a change occurs, the value is not immediately recomputed.
                    Recomputation happens later, if and when the value is subsequently requested.
                    In Example 6, the bill total (a binding) will be marked as invalid the first time it detects
                    a change in one of its dependencies. However, the binding object will recalculate itself
                    only if the total is actually requested again.

                    Example 1–6 Using an InvalidationListener
                    package bindingdemo;

                    import javafx.beans.property.DoubleProperty;
                    import
                    javafx.beans.property.SimpleDoubleProperty;
                    import javafx.beans.binding.NumberBinding;
                    import javafx.beans.binding.Bindings;
                    import javafx.beans.InvalidationListener;
                    import javafx.beans.Observable;

                    class Bill {

                        // Define the property
                        private DoubleProperty amountDue = new
                    SimpleDoubleProperty();

                         // Define a getter for the property's value
                         public final double getAmountDue(){return amountDue.get();}

                         // Define a setter for the property's value
                         public final void setAmountDue(double value){amountDue.set(value);}

                          // Define a getter for the property itself
                         public DoubleProperty amountDueProperty() {return amountDue;}

                    }

                    public class Main {

                         public static void main(String[] args) {

                              Bill bill1 = new Bill();
                              Bill bill2 = new Bill();
                              Bill bill3 = new Bill();

                              NumberBinding total =
                                Bindings.add(bill1.amountDueProperty().add(bill2.amountDueProperty()),
                                    bill3.amountDueProperty());
                              total.addListener(new InvalidationListener() {


1-6 Oracle JavaFX/Using JavaFX Properties and Binding
Using the Low-Level Binding API



                   @Override public void
           invalidated(Observable o) {
                           System.out.println("The binding is now invalid.");
                       }
                   });

                   // First call makes the binding invalid
                   bill1.setAmountDue(200.00);

                   // The binding is now invalid
                   bill2.setAmountDue(100.00);
                   bill3.setAmountDue(75.00);

                   // Make the binding valid...
                   System.out.println(total.getValue());

                   // Make invalid...
                   bill3.setAmountDue(150.00);

                   // Make valid...
                   System.out.println(total.getValue());
               }
           }

           By changing the value of a single bill, the binding becomes invalid, and the
           invalidation listener will fire. But if the binding is already invalid, the invalidation
           listener will not fire again, even if another bill changes. (In Example 6, invoking
           total.getValue() moves the binding from invalid to valid.) We know this
           because a subsequent change to any bill in the dependency list will cause the
           invalidation listener to fire again. This would not happen if the binding was still
           invalid.
           Note that registering a ChangeListener will enforce eager computation, even if the
           implementation of the ObservableValue supports lazy evaluation. For a lazily
           evaluated value, it is not possible to know if an invalid value really has changed until
           it is recomputed. For this reason, generating change events requires eager evaluation,
           while invalidation events can be generated for both eager and lazy implementations.


Using the Low-Level Binding API
           If the High-Level API is not enough to satisfy your requirements, you can always use
           the Low-Level API instead. The Low-Level API is for developers who require more
           flexibility (or better performance) than that offered by the High-Level API.
           Example 7 shows a basic example of using the
           Low-Level API.

           Example 1–7 Using the Low-Level API
           package bindingdemo;

           import javafx.beans.property.DoubleProperty;
           import
           javafx.beans.property.SimpleDoubleProperty;
           import javafx.beans.binding.DoubleBinding;

           public class Main {




                                                              Using JavaFX Properties and Binding 1-7
Using the Low-Level Binding API


                         public static void main(String[] args) {

                            final DoubleProperty   a = new
                    SimpleDoubleProperty(1);
                            final DoubleProperty   b = new
                    SimpleDoubleProperty(2);
                            final DoubleProperty   c = new
                    SimpleDoubleProperty(3);
                            final DoubleProperty   d = new
                    SimpleDoubleProperty(4);

                             DoubleBinding db = new DoubleBinding() {

                                  {
                                      super.bind(a, b, c, d);
                                  }

                                @Override
                                protected double computeValue() {
                                    return (a.get() * b.get()) +
                    (c.get() * d.get());
                                }
                            };

                             System.out.println(db.get());
                             b.set(3);
                             System.out.println(db.get());
                         }
                    }

                    Using the Low-Level API involves extending one of the binding classes and overriding
                    its computeValue() method to return the current value of the binding. Example 7
                    does this with a custom subclass of DoubleBinding. The invocation of
                    super.bind() passes the dependencies up to DoubleBinding so that the default
                    invalidation behavior is retained. It is generally not necessary for you to check if the
                    binding is invalid; this behavior is provided for you by the base class.
                    You now know enough information to begin using
                    the Low-Level API.




1-8 Oracle JavaFX/Using JavaFX Properties and Binding

More Related Content

What's hot (20)

PDF
Introduction to Java EE (J2EE)
Atit Patumvan
 
PPSX
CR Bridge Solutions Pvt Ltd. Java slides
CRBTech
 
PDF
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Edureka!
 
PPTX
Software Design Principles (SOLID)
ASIMYILDIZ
 
PDF
Java technologies explained to non-technical audience
Steinn 'Stan' Jónsson
 
ODT
Spring framework
Shivi Kashyap
 
PDF
Hibernate Interview Questions
Syed Shahul
 
DOCX
Spring notes
Rajeev Uppala
 
DOCX
159747608 a-training-report-on
homeworkping7
 
PPTX
J2ee architecture
Erencan Özkan
 
PDF
Tutorial for netbeans
Rudresh Shrivastav
 
PPT
Spring Framework
nomykk
 
PPT
Java Programming for Designers
R. Sosa
 
PDF
Java EE7 Demystified
Ankara JUG
 
PPT
Java for Recruiters
ph7 -
 
PDF
Java Interview Questions and Answers | Spring and Hibernate Interview Questio...
Edureka!
 
PPTX
Intro to Java
karianneban
 
PPTX
Introduction Java Web Framework and Web Server.
suranisaunak
 
PPTX
java tutorial for beginner - Free Download
TIB Academy
 
PDF
Learn java in hindi
Vipin sharma
 
Introduction to Java EE (J2EE)
Atit Patumvan
 
CR Bridge Solutions Pvt Ltd. Java slides
CRBTech
 
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Edureka!
 
Software Design Principles (SOLID)
ASIMYILDIZ
 
Java technologies explained to non-technical audience
Steinn 'Stan' Jónsson
 
Spring framework
Shivi Kashyap
 
Hibernate Interview Questions
Syed Shahul
 
Spring notes
Rajeev Uppala
 
159747608 a-training-report-on
homeworkping7
 
J2ee architecture
Erencan Özkan
 
Tutorial for netbeans
Rudresh Shrivastav
 
Spring Framework
nomykk
 
Java Programming for Designers
R. Sosa
 
Java EE7 Demystified
Ankara JUG
 
Java for Recruiters
ph7 -
 
Java Interview Questions and Answers | Spring and Hibernate Interview Questio...
Edureka!
 
Intro to Java
karianneban
 
Introduction Java Web Framework and Web Server.
suranisaunak
 
java tutorial for beginner - Free Download
TIB Academy
 
Learn java in hindi
Vipin sharma
 

Similar to Jfxpub binding (20)

PDF
Spring 2
Aruvi Thottlan
 
PDF
Spring Framework Tutorial | VirtualNuggets
Virtual Nuggets
 
PDF
Learn Advanced Java Programming With Beginners Md Pulok
lwafaziurka
 
PPTX
Java seminar
devendrakhairwa
 
PDF
Java Introduction | PDF
Geekster
 
PPTX
List of 7 popular java frameworks for 2019
kritikumar16
 
PPTX
List of 7 popular java frameworks for 2019
kritikumar16
 
PPTX
core and advanced java presentation for submission
arshdeepkaurkaur5368
 
PPTX
Java spring ppt
natashasweety7
 
PPTX
Java-Development-A-Comprehensive-Guide.pptx
ak7209566972
 
PPT
Rollin onj Rubyv3
Oracle
 
PPTX
Introduction to Spring sec1.pptx
NourhanTarek23
 
PDF
Java Coaching in Hyderabad introduction
Azure Data Factory
 
PPT
10 interesting things about java
kanchanmahajan23
 
PDF
www-codemechsolutions-com-blog-details-features-of-java....pdf
Codemech Solutions
 
PPTX
Introduction to java
Veerabadra Badra
 
PDF
Java programming material for beginners by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
PDF
Java notes
Debasish Biswas
 
PDF
The Brainify App - JavaFx
Mohd Shamweel
 
PPTX
Introduction to Spring Framework
Dineesha Suraweera
 
Spring 2
Aruvi Thottlan
 
Spring Framework Tutorial | VirtualNuggets
Virtual Nuggets
 
Learn Advanced Java Programming With Beginners Md Pulok
lwafaziurka
 
Java seminar
devendrakhairwa
 
Java Introduction | PDF
Geekster
 
List of 7 popular java frameworks for 2019
kritikumar16
 
List of 7 popular java frameworks for 2019
kritikumar16
 
core and advanced java presentation for submission
arshdeepkaurkaur5368
 
Java spring ppt
natashasweety7
 
Java-Development-A-Comprehensive-Guide.pptx
ak7209566972
 
Rollin onj Rubyv3
Oracle
 
Introduction to Spring sec1.pptx
NourhanTarek23
 
Java Coaching in Hyderabad introduction
Azure Data Factory
 
10 interesting things about java
kanchanmahajan23
 
www-codemechsolutions-com-blog-details-features-of-java....pdf
Codemech Solutions
 
Introduction to java
Veerabadra Badra
 
Java programming material for beginners by Nithin, VVCE, Mysuru
Nithin Kumar,VVCE, Mysuru
 
Java notes
Debasish Biswas
 
The Brainify App - JavaFx
Mohd Shamweel
 
Introduction to Spring Framework
Dineesha Suraweera
 
Ad

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
PCU Keynote at IEEE World Congress on Services 250710.pptx
Ramesh Jain
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PDF
Basics of Electronics for IOT(actuators ,microcontroller etc..)
arnavmanesh
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PDF
SalesForce Managed Services Benefits (1).pdf
TechForce Services
 
PDF
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PPTX
Using Google Data Studio (Looker Studio) to Create Effective and Easy Data Re...
Orage Technologies
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PCU Keynote at IEEE World Congress on Services 250710.pptx
Ramesh Jain
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
Basics of Electronics for IOT(actuators ,microcontroller etc..)
arnavmanesh
 
The Future of Artificial Intelligence (AI)
Mukul
 
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
SalesForce Managed Services Benefits (1).pdf
TechForce Services
 
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
Using Google Data Studio (Looker Studio) to Create Effective and Easy Data Re...
Orage Technologies
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Ad

Jfxpub binding

  • 1. Oracle® JavaFX Using JavaFX Properties and Binding Release 2.1 E20469-02 April 2012
  • 2. Oracle JavaFX/Using JavaFX Properties and Binding, Release 2.1 E20469-02 Copyright © 2011,2012 Oracle and/or its affiliates. All rights reserved. Primary Author: Scott Hommel Contributing Author: Contributor: This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, the following notice is applicable: U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are "commercial computer software" or "commercial technical data" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, duplication, disclosure, modification, and adaptation shall be subject to the restrictions and license terms set forth in the applicable Government contract, and, to the extent applicable by the terms of the Government contract, the additional rights set forth in FAR 52.227-19, Commercial Computer Software License (December 2007). Oracle America, Inc., 500 Oracle Parkway, Redwood City, CA 94065. This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark licensed through X/Open Company, Ltd. This software or hardware and documentation may provide access to or information on content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services.
  • 3. Contents 1 Using JavaFX Properties and Binding Overview.................................................................................................................................................... 1-1 Understanding Properties....................................................................................................................... 1-1 Using the High-Level Binding API....................................................................................................... 1-4 Exploring Observable, ObservableValue, InvalidationListener, and ChangeListener .............. 1-6 Using the Low-Level Binding API ........................................................................................................ 1-7 iii
  • 4. iv
  • 5. 1 Using JavaFX Properties 1 and Binding This tutorial describes properties and binding in JavaFX 2.0, using working examples that you can compile and run. Overview For many years, the Java programming language has used the JavaBeans component architecture to represent the property of an object. This model consists of both an API and a design pattern; it is widely understood by Java application developers and development tools alike. This release introduces property support into JavaFX, support that is based on the proven JavaBeans model, but expanded and improved. JavaFX properties are often used in conjunction with binding, a powerful mechanism for expressing direct relationships between variables. When objects participate in bindings, changes made to one object will automatically be reflected in another object. This can be useful in a variety of applications. For example, binding could be used in a bill invoice tracking program, where the total of all bills would automatically be updated whenever an individual bill is changed. Or, binding could be used in a graphical user interface (GUI) that automatically keeps its display synchronized with the application's underlying data. Bindings are assembled from one or more sources, known as dependencies. A binding observes its list of dependencies for changes, and then updates itself automatically after a change has been detected. The binding APIs are divided into two broad categories: 1. The High-Level API: Provides a simple way to create bindings for the most common use cases. Its syntax is easy to learn and use, especially in environments that provide code completion, such as the NetBeans IDE. 2. The Low-Level API: Provides additional flexibility, and can be used by advanced developers in situations where the High-Level API is insufficient. The Low-Level API was designed for fast execution and small memory footprint. The remainder of this tutorial describes these APIs, and provides working code examples that you can compile and run. Understanding Properties As mentioned in the overview, JavaFX property support is based on the well-known property model established by the JavaBeans component architecture. This section Using JavaFX Properties and Binding 1-1
  • 6. Understanding Properties provides a brief overview of what that means, then explains how properties apply to JavaFX. The Java programming language makes it possible to encapsulate data within an object, but it does not enforce any specific naming conventions for the methods that you define. For example, your code might define a Person class, which encapsulates a first name and a last name. But without naming conventions, different programmers might choose different names for these methods: read_first(), firstName(), getFN(), etc. would all be perfectly valid choices. However, there is no guarantee that these names will be meaningful to other developers. The JavaBeans component architecture addressed this problem by defining some simple naming conventions that bring consistency across projects. In JavaBeans programming, the full signatures for these methods would be: public void setFirstName(String name), public String getFirstName(), public void setLastName(String name), and public String getLastName(). This naming pattern is easily recognizable, both to human programmers and to editing tools, such as the NetBeans IDE. In JavaBeans terminology, the Person object is said to contain firstName and lastName properties. The JavaBeans model also provides support for complex property types, plus an event delivery system. It also contains a number of support classes, all available as an API under the java.beans package. Therefore, mastering JavaBeans programming involves learning the required naming conventions and its corresponding API. (For more background reading on JavaBeans in general, see the JavaBeans lesson of the Java Tutorial at http://download.oracle.com/javase/tutorial/javabeans). Similarly, understanding JavaFX properties also requires learning a few new APIs and naming conventions. In JavaFX, it is entirely possible that you will only be interested in using classes that contain properties (as opposed to implementing properties in your own custom classes), but Example 1 will familiarize you with the new method naming conventions that form the JavaFX property pattern. It defines a class named Bill, which implements a single property named amountDue. Example 1–1 Defining a Property package propertydemo; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; class Bill { // Define a variable to store the property private DoubleProperty amountDue = new SimpleDoubleProperty(); // Define a getter for the property's value public final double getAmountDue(){return amountDue.get();} // Define a setter for the property's value public final void setAmountDue(double value){amountDue.set(value);} // Define a getter for the property itself public DoubleProperty amountDueProperty() {return amountDue;} } 1-2 Oracle JavaFX/Using JavaFX Properties and Binding
  • 7. Understanding Properties The amountDue object — an instance of the javafx.beans.property.DoubleProperty class — is marked as private to encapsulate it from the outside world. This is standard practice in both Java and JavaBeans application development. Note however that the object’s type is not one of the standard Java primitives, but rather, a new wrapper class that encapsulates a Java primitive and adds some extra functionality (the classes under javafx.beans.property all contain built-in support for observability and binding as part of their design). The property method naming conventions are as follows: ■ The getAmountDue() method is a standard getter that returns the current value of the amountDue property. By convention, this method is declared as final. Note that the return type for this method is double, not DoubleProperty. ■ The setAmountDue(double) method (also final) is a standard setter that allows a caller to set the property's value. The setter method is optional. Its parameter is also of type double. ■ Finally, the amountDueProperty() method defines the property getter. This is a new convention in which the method name contains the name of the property (amountDue, in this case), followed by the word "Property." The return type is the same as the property itself (DoubleProperty, in this example). When building GUI applications with JavaFX, you will notice that certain classes in the API already implement properties. For example, the javafx.scene.shape.Rectangle class contains properties for arcHeight, arcWidth, height, width, x, and y. For each of these properties there will be corresponding methods that match the conventions previously described. For example, getArcHeight(), setArcHeight(double), arcHeightProperty(), which together indicate (to both developers and tools) that the given property exists. You can also add a change listener to be notified when the property's value has changed, as shown in Example 2. Example 1–2 Using a ChangeListener package propertydemo; import javafx.beans.value.ObservableValue; import javafx.beans.value.ChangeListener; public class Main { public static void main(String[] args) { Bill electricBill = new Bill(); electricBill.amountDueProperty().addListener(new ChangeListener(){ @Override public void changed(ObservableValue o,Object oldVal, Object newVal){ System.out.println("Electric bill has changed!"); } }); electricBill.setAmountDue(100.00); } } Using JavaFX Properties and Binding 1-3
  • 8. Using the High-Level Binding API Running this example will print the message "Electric bill has changed" to standard output, proving that the change listener notification is working. Using the High-Level Binding API The High-Level API is the quickest and easiest way to begin using bindings in your own applications. It consists of two parts: the Fluent API, and the Bindings class. The Fluent API exposes methods on the various dependency objects, whereas the Bindings class provides static factory methods instead. To begin using the Fluent API, consider a simple use case in which two integers are bound so that their values are always added together. In Example 3, there are three variables involved: num1 (a dependency), num2 (a dependency), and sum (the binding). The dependency types are both IntegerProperty, and the binding itself is NumberBinding. Example 1–3 Using the Fluent API package bindingdemo; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.binding.NumberBinding; public class Main { public static void main(String[] args) { IntegerProperty num1 = new SimpleIntegerProperty(1); IntegerProperty num2 = new SimpleIntegerProperty(2); NumberBinding sum = num1.add(num2); System.out.println(sum.getValue()); num1.set(2); System.out.println(sum.getValue()); } } This code binds the two dependencies, prints their sum, then changes the value of num1 and prints the sum again. The results are "3" and "4", which proves that the binding is working. You could also use the Bindings class to do the same thing, as shown in Example 4. Example 1–4 Using the Bindings Class package bindingdemo; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.binding.NumberBinding; import javafx.beans.binding.Bindings; public class Main { public static void main(String[] args) { 1-4 Oracle JavaFX/Using JavaFX Properties and Binding
  • 9. Using the High-Level Binding API IntegerProperty num1 = new SimpleIntegerProperty(1); IntegerProperty num2 = new SimpleIntegerProperty(2); NumberBinding sum = Bindings.add(num1,num2); System.out.println(sum.getValue()); num1.setValue(2); System.err.println(sum.getValue()); } } Example 5 combines the two approaches: Example 1–5 Combining Both Approaches package bindingdemo; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.binding.NumberBinding; import javafx.beans.binding.Bindings; public class Main { public static void main(String[] args) { IntegerProperty num1 = new SimpleIntegerProperty(1); IntegerProperty num2 = new SimpleIntegerProperty(2); IntegerProperty num3 = new SimpleIntegerProperty(3); IntegerProperty num4 = new SimpleIntegerProperty(4); NumberBinding total = Bindings.add(num1.multiply(num2),num3.multiply(num4)); System.out.println(total.getValue()); num1.setValue(2); System.err.println(total.getValue()); } } Example 5 modifies the code to invoke the multiply method from the Fluent API, and add from the Bindings class. You should also know that the High-Level API lets you mix types when defining arithmetic operations. The type of the result is defined by the same rules as the Java programming language: 1. If one of the operands is a double, the result is a double. 2. If not and one of the operands is a float, the result is a float. 3. If not and one of the operands is a long, the result is a long. 4. The result is an integer otherwise. The next section explores observability, and demonstrates how invalidation listeners differ from change listeners. Using JavaFX Properties and Binding 1-5
  • 10. Exploring Observable, ObservableValue, InvalidationListener, and ChangeListener Exploring Observable, ObservableValue, InvalidationListener, and ChangeListener The binding API defines a set of interfaces that enable objects to be notified when a value change or invalidation takes place. The Observable and ObservableValue interfaces fire the change notifications, and the InvalidationListener and ChangeListener interfaces receive them. The difference between the two is that ObservableValue wraps a value and fires its changes to any registered ChangeListener, whereas Observable (which does not wrap a value) fires its changes to any registered InvalidationListener. The JavaFX binding and property implementations all support lazy evaluation, which means that when a change occurs, the value is not immediately recomputed. Recomputation happens later, if and when the value is subsequently requested. In Example 6, the bill total (a binding) will be marked as invalid the first time it detects a change in one of its dependencies. However, the binding object will recalculate itself only if the total is actually requested again. Example 1–6 Using an InvalidationListener package bindingdemo; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.binding.NumberBinding; import javafx.beans.binding.Bindings; import javafx.beans.InvalidationListener; import javafx.beans.Observable; class Bill { // Define the property private DoubleProperty amountDue = new SimpleDoubleProperty(); // Define a getter for the property's value public final double getAmountDue(){return amountDue.get();} // Define a setter for the property's value public final void setAmountDue(double value){amountDue.set(value);} // Define a getter for the property itself public DoubleProperty amountDueProperty() {return amountDue;} } public class Main { public static void main(String[] args) { Bill bill1 = new Bill(); Bill bill2 = new Bill(); Bill bill3 = new Bill(); NumberBinding total = Bindings.add(bill1.amountDueProperty().add(bill2.amountDueProperty()), bill3.amountDueProperty()); total.addListener(new InvalidationListener() { 1-6 Oracle JavaFX/Using JavaFX Properties and Binding
  • 11. Using the Low-Level Binding API @Override public void invalidated(Observable o) { System.out.println("The binding is now invalid."); } }); // First call makes the binding invalid bill1.setAmountDue(200.00); // The binding is now invalid bill2.setAmountDue(100.00); bill3.setAmountDue(75.00); // Make the binding valid... System.out.println(total.getValue()); // Make invalid... bill3.setAmountDue(150.00); // Make valid... System.out.println(total.getValue()); } } By changing the value of a single bill, the binding becomes invalid, and the invalidation listener will fire. But if the binding is already invalid, the invalidation listener will not fire again, even if another bill changes. (In Example 6, invoking total.getValue() moves the binding from invalid to valid.) We know this because a subsequent change to any bill in the dependency list will cause the invalidation listener to fire again. This would not happen if the binding was still invalid. Note that registering a ChangeListener will enforce eager computation, even if the implementation of the ObservableValue supports lazy evaluation. For a lazily evaluated value, it is not possible to know if an invalid value really has changed until it is recomputed. For this reason, generating change events requires eager evaluation, while invalidation events can be generated for both eager and lazy implementations. Using the Low-Level Binding API If the High-Level API is not enough to satisfy your requirements, you can always use the Low-Level API instead. The Low-Level API is for developers who require more flexibility (or better performance) than that offered by the High-Level API. Example 7 shows a basic example of using the Low-Level API. Example 1–7 Using the Low-Level API package bindingdemo; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.binding.DoubleBinding; public class Main { Using JavaFX Properties and Binding 1-7
  • 12. Using the Low-Level Binding API public static void main(String[] args) { final DoubleProperty a = new SimpleDoubleProperty(1); final DoubleProperty b = new SimpleDoubleProperty(2); final DoubleProperty c = new SimpleDoubleProperty(3); final DoubleProperty d = new SimpleDoubleProperty(4); DoubleBinding db = new DoubleBinding() { { super.bind(a, b, c, d); } @Override protected double computeValue() { return (a.get() * b.get()) + (c.get() * d.get()); } }; System.out.println(db.get()); b.set(3); System.out.println(db.get()); } } Using the Low-Level API involves extending one of the binding classes and overriding its computeValue() method to return the current value of the binding. Example 7 does this with a custom subclass of DoubleBinding. The invocation of super.bind() passes the dependencies up to DoubleBinding so that the default invalidation behavior is retained. It is generally not necessary for you to check if the binding is invalid; this behavior is provided for you by the base class. You now know enough information to begin using the Low-Level API. 1-8 Oracle JavaFX/Using JavaFX Properties and Binding