Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
JavaFX background image load tracking example.
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.StackPaneBuilder;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/** Demo of tracking the loading of background images in JavaFX */
public class ImageLoadTracker extends Application {
private static final String SHALLOT_IMAGE_URL = "";
private static final String BROKEN_IMAGE_URL = "";
private static final Image ERROR_IMAGE = new Image(""); // linkware icon from
@Override public void start(final Stage stage) {
// a label for reporting the current image loading status.
final Label statusLabel = new Label();
// a progressbar to monitor the progress of background image loading.
final Label progressLabel = new Label("Loading progress");
final ProgressBar progressBar = new ProgressBar();
// a view to hold the loaded image.
final ImageView imageView = new ImageView();
// radio buttons to control image loading.
final RadioButton shallotImageSelection = new RadioButton("Load a nice image");
final RadioButton brokenImageSelection = new RadioButton("Load a broken image");
// a toggle group to load images when the user toggles between the toggles.
ToggleGroup imageToggle = new ToggleGroup();
imageToggle.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
@Override public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
// reset the error text.
statusLabel.setText("Loading image . . .");
statusLabel.setStyle("-fx-text-fill: silver;");
// load an image in the background.
final String newImageUrl = (String) newValue.getUserData();
final Image newImage = new Image(newImageUrl, true);
// track the image's error property.
newImage.errorProperty().addListener(new ChangeListener<Boolean>() {
@Override public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean imageError) {
if (imageError) {
statusLabel.setText("Oh-oh there was an error loading: " + newImageUrl);
statusLabel.setStyle("-fx-text-fill: firebrick;");
// track the image's loading progress.
newImage.progressProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number progress) {
if ((Double) progress == 1.0 && ! newImage.isError()) {
statusLabel.setText("Loading complete");
statusLabel.setStyle("-fx-text-fill: forestgreen;");
// layout the scene.
StackPane imageLayout = StackPaneBuilder.create().children(imageView).build();
VBox layout = new VBox(10);
HBoxBuilder.create().spacing(10).alignment(Pos.CENTER).children(progressLabel, progressBar).build(),
VBox.setVgrow(imageLayout, Priority.ALWAYS);
HBox.setHgrow(progressBar, Priority.ALWAYS);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
stage.setTitle("Image Loading Tracker");
stage.setScene(new Scene(layout, 1315, 1000));;
// trigger the loading of a good image.;
public static void main(String[] args) { launch(); }
Copy link

jewelsea commented Apr 30, 2012

Copy link

MenaiAla commented Jan 12, 2018

@jwelsea ,this example helps me ,thank you :)

Copy link

ranjitvamadevan commented Apr 7, 2021

this program is not working for me, kindly help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment