Java Reference
In-Depth Information
F Loading the image —the image is loaded by calling the function loadImg() . It
assigns an instance of Image to imgView.image . For the Image.url property,
we use the Scene.lookup(id:String) function to retrieve an instance of the
TextBox using its id of addr . For images that may take a while to load, we set
up the following two properties:
To ensure that the application does not hang while the image loads,
the property backgroundLoading:Boolean is set to true. This
causes the GUI to remain responsive while an image loads.
The property placeholder:Image is used to specify a local image
to use while the remote image is loading, as shown in the previous
screenshot. For example, we use the local image {__DIR__}
loading.png . It gets loaded immediately and remains on the
screen while the remote image loads. When the remote image is
loaded, it replaces the placeholder image.
There's more...
Format support
As of version 1.2, JavaFX has inherent supports for the most popular image formats
(popularity here = web-supported), which includes PNG , JPG , BMP , and GIF . If you have
requirements for formats other than these, such as TIFF for instance, you will have to take
matters into your own hands and use external image libraries such as Java-Advanced-
Imaging (JAI) API (not covered here).
Asynchronous loading issues
As mentioned in the previous section, when you are loading images from locations with high
latency (over the network for instance), you can use the asynchronous background-loading
option for your image. This causes the image-loading operation to occur in a separate
execution thread to keep your GUI responsive.
This, however, presents an issue, whereby if you want to determine the dimensions of the
image (which is available only after the image is fully downloaded), it will report zero when
loading asynchronously, as shown in the next segment:
def img = Image{
}//does not wait here, it continues to next line
println (img.width); // prints 0
This is because the image is still being downloaded on the Image thread, and the main GUI
thread did not wait for completion and continues with its execution. Therefore, when we query
the property width of Image , it will be zero.
Search WWH ::

Custom Search