Properties and Binding
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Properties</strong><br />
<strong>and</strong><br />
<strong>Binding</strong><br />
Dr. Ahmad Zaeri<br />
Faculty of Computer Engineering<br />
University of Isfahan
JavaBean<br />
• Default Constructor<br />
• Property<br />
• Private Field<br />
• Setter<br />
• Getter<br />
• Use Naming Conventions !<br />
2
JavaFX Property<br />
• A class that contains properties with some new method naming conventions !<br />
Returns the current value<br />
of the amountDue property<br />
Set the value property<br />
of the amountDue property<br />
New convention !<br />
Returns the amountDue property itself !<br />
3
Built-in <strong>Properties</strong><br />
• javafx.beans.property.*<br />
• Classes, which end in “Property”<br />
• Classes that all have the ability to be bound<br />
IntegerProperty<br />
BooleanProperty<br />
StringProperty<br />
ListProperty<br />
ObjectProperty<br />
4
<strong>Binding</strong><br />
• When objects participate in bindings, changes made to one object will<br />
automatically be reflected in another object.<br />
• To synchronize changes between two values.<br />
• There are many types of binding so …<br />
5
• High level :<br />
• Fluent API<br />
Types of binding<br />
• A Simple Approach<br />
• <strong>Binding</strong> Class<br />
• High/Low level :<br />
a simple way to create bindings<br />
• ChangeListener Interface<br />
• InvalidationListener Interface<br />
• Low level :<br />
• Using one of the binding classes <strong>and</strong> overriding its computeValue()<br />
method<br />
6
High Level :<br />
A Simple Approach …<br />
• Unidirectional <strong>Binding</strong>:<br />
Direct change in x causes exception!<br />
7
High Level :<br />
A Simple Approach …(cont’d)<br />
• Bidirectional <strong>Binding</strong><br />
8
High Level:<br />
Fluent API<br />
Exposes methods on the various dependency objects .<br />
9
High Level:<br />
<strong>Binding</strong>s Class<br />
Provides static factory methods.<br />
10
Key interfaces of the<br />
JavaFX properties <strong>and</strong> bindings framework<br />
11
Key interfaces of the<br />
JavaFX properties <strong>and</strong> bindings framework(cont’d<br />
• The Observable interface fires the change notifications<br />
<strong>and</strong><br />
the InvalidationListener interface receives them.<br />
• The ObservableValue interface fires the change notifications<br />
<strong>and</strong><br />
the ChangeListener interface receives them.<br />
12
High/Low Level :<br />
Eagerly…<br />
Add a change listener to be notified when the property's value has changed.<br />
See slide 3 !<br />
13
High/Low Level :<br />
Lazily…<br />
• By changing the value of a property, the binding becomes invalid, <strong>and</strong> the<br />
invalidation listener will fire.<br />
• When a change occurs, the value is not immediately recomputed.<br />
• Recomputation happens later, if <strong>and</strong> when the value is subsequently requested.<br />
14
High/Low Level :<br />
Lazily…<br />
Makes the binding invalid<br />
The binding is now invalid<br />
The binding is now invalid<br />
Make the binding valid<br />
Make invalid …<br />
Make valid …<br />
15
Low Level:<br />
This allows the developer to use primitives <strong>and</strong> more-complex math operations.<br />
• Use a derived Number<strong>Binding</strong> class such as a Double<strong>Binding</strong> class.<br />
• Override its computeValue() method<br />
• you can use the familiar operators such as * <strong>and</strong> - to formulate complex<br />
math equations:<br />
16
Low Level:<br />
Example …<br />
17
Another Example …<br />
See <strong>Binding</strong>Expressions.java Example in the “<strong>Properties</strong> <strong>and</strong><br />
<strong>Binding</strong>” package.<br />
18
Now let’s see <strong>Binding</strong> in<br />
LogEdit !<br />
19
Resources<br />
• JavaFX <strong>Properties</strong> <strong>and</strong> <strong>Binding</strong> Tutorial, Release 8<br />
• JavaFX 2.0 Introduction By Example<br />
• Pro JavaFX 2.0<br />
20