Java Reference
In-Depth Information
F Display and add interaction —once the images are loaded in the images[] sequence,
we can now create the ImageView object assigned to variable imgView that will
display the images.
The ImageView instance requests focus using the imgView.
requestFocus() function call. This causes the imgView
component to receive keyboard events.
A keyboard input event-handler function is attached to imgView
to handle keyboard key presses. The code loops through the
sequence of images to look for an Image instance whose url
property contains the value of the key pressed, as shown in the
following snippet:
for(i in images){
if(i.url.contains("{e.text}.png")) {
img = i;
break;
}
}
When the image is found, the value is assigned to imgView.image
for display.
There's more...
A general approach to loading multiple images automatically is to use a predictable sequence
to name your image files. For instance, for this recipe, we can use a numeric sequence of
integers as a part of the file name instead of just the alphabet sequence of letters. So, the
image for letter A is named letter_0.png , the image for letter B is named letter_1.png ,
and so forth, with the image for letter Z named letter_25.png . By doing this, our code gets
even smaller, where we no longer need the sequence alphabet[] to store the image names.
We can load the images as follows:
for (id in [0..25]){
insert Image {
url: "{__DIR__}images/letter_{id}.png"
backgroundLoading: false
width:imgW height:imgH
} into images;
}
See also
F Chapter 1 Creating and using JavaFX sequences
F Chapter 5 Loading and displaying images with ImageView
 
Search WWH ::




Custom Search