Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Created April 30, 2012 07:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jewelsea/2556122 to your computer and use it in GitHub Desktop.
Save jewelsea/2556122 to your computer and use it in GitHub Desktop.
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 = "http://upload.wikimedia.org/wikipedia/commons/8/83/JWW_TheLadyOfShallot_1888.jpg";
private static final String BROKEN_IMAGE_URL = "http://error.org/imagedoesnotexist.jpg";
private static final Image ERROR_IMAGE = new Image("http://icons.iconarchive.com/icons/visualpharm/must-have/256/Cancel-icon.png"); // linkware icon from http://www.visualpharm.com/
@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");
shallotImageSelection.setUserData(SHALLOT_IMAGE_URL);
brokenImageSelection.setUserData(BROKEN_IMAGE_URL);
// a toggle group to load images when the user toggles between the toggles.
ToggleGroup imageToggle = new ToggleGroup();
shallotImageSelection.setToggleGroup(imageToggle);
brokenImageSelection.setToggleGroup(imageToggle);
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);
imageView.setImage(newImage);
// 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;");
imageView.setImage(ERROR_IMAGE);
}
}
});
// track the image's loading progress.
progressBar.progressProperty().bind(newImage.progressProperty());
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);
layout.getChildren().addAll(
shallotImageSelection,
brokenImageSelection,
statusLabel,
HBoxBuilder.create().spacing(10).alignment(Pos.CENTER).children(progressLabel, progressBar).build(),
imageLayout
);
VBox.setVgrow(imageLayout, Priority.ALWAYS);
progressBar.setMaxWidth(Double.MAX_VALUE);
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));
stage.show();
// trigger the loading of a good image.
shallotImageSelection.fire();
}
public static void main(String[] args) { launch(); }
}
@jewelsea
Copy link
Author

@alamenai
Copy link

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

@ranjitvamadevan
Copy link

this program is not working for me, kindly help.

@ctoabidmaqbool
Copy link

@jewelsea Hi! Thanks for providing such a nice solutions;

What If we have image as inputstram e.g. from database. Then how can such background loading of image be achieved?

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