Java Reference
In-Depth Information
Upon execution, this program displays an editable TextField containing the text
“Change me”. Unfortunately, if you try and type anything inside that TextField,
an AssignToBoundException will be thrown. Why? Because whenever the text
inside the TextField is changed, its text instance variable changes automatically too.
Because text is bound to str , this activity tries to reassign a bound variable caus-
ing the exception. Initially, the application appears like that shown in Figure 4.2.
Figure 4.2
Initial Appearance of the TextField
Figure 4.3 shows what happens when you click on the TextField and clear the
string.
Figure 4.3
Clearing the TextField Throws an Exception
In situations like this, it would be nice if the binding relationship between str
and text was bidirectional—that is, a change to either variable would automati-
cally update the other. This is possible in JavaFX by using the with inverse
phrase when binding. By replacing
text: bind str
with
text: bind str with inverse
a bidirectional relationship is created. With this modification, you'll now be able
to modify the TextField without worry. When the program begins, the text
instance variable is assigned the value of str (“Change me”), and when you
change the contents of the TextField, its text instance variable and the str vari-
able will reflect the change. Listing 4.3 contains the new version of the program.
Search WWH ::




Custom Search