Java Reference
In-Depth Information
at com.sun.javafx.runtime.location.IntVariable.setAsInt
(IntVariable.java:115)
at main.javafx$run$(main.fx:4)
at main.javafx$run$(main.fx:4)
One way to avoid the previously noted exception would be to use the
def
key-
word, rather than
var
, to define the binding relationship between
x
and
y
. If
y
were to be defined like
def y = bind x;
the attempt to reassign
y
would instead be caught by the JavaFX compiler and
flagged as an error at compile time. As an aside, we'll see later on in this chapter
that the reassignment restrictions mentioned in this section do not apply when
using the
bind with inverse
clause.
Finally, you cannot bind to an operation that would produce side effects. For
example,
var x = bind y++;
is not permitted.
We have thus far been highlighting
bind
's capabilities by examining small
chunks of code. This affords us the ability to focus on the task at hand without
having to worry about additional clutter; however, it does not necessarily provide
the proper context for how
bind
might be used in a (slightly) more realistic
application. So let's apply what we've learned so far to show how binding can
affect what will actually show up on the screen.
The program in Listing 4.1 always displays an image, the contents of which will
change depending upon where the mouse is currently located. While running,
this program has two states:
1. When the mouse is hovering directly over the image, the image displayed
will include text that suggests that in order to change this image, move
your mouse outside the image.
2. When the mouse is not hovering over the image, the image displayed will
include text which suggests that if you want the current image to change,
run your mouse over it.
Search WWH ::
Custom Search