Created
January 10, 2012 11:19
-
-
Save jewelsea/1588531 to your computer and use it in GitHub Desktop.
JavaFX standalone application Splash Page for a great website.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jewelsea.examples.splash; | |
import javafx.animation.FadeTransition; | |
import javafx.application.Application; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Pos; | |
import javafx.geometry.Rectangle2D; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.ProgressBar; | |
import javafx.scene.effect.DropShadow; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.web.WebView; | |
import javafx.stage.Screen; | |
import javafx.stage.Stage; | |
import javafx.stage.StageStyle; | |
import javafx.util.Duration; | |
import org.w3c.dom.Document; | |
/** Example of displaying a splash page for a standalone JavaFX application */ | |
public class FxExperienceSplash extends Application { | |
private Pane splashLayout; | |
private ProgressBar loadProgress; | |
private Label progressText; | |
private WebView webView; | |
private Stage mainStage; | |
private static final int SPLASH_WIDTH = 676; | |
private static final int SPLASH_HEIGHT = 227; | |
public static void main(String[] args) throws Exception { launch(args); } | |
@Override public void init() { | |
ImageView splash = new ImageView(new Image("http://fxexperience.com/wp-content/uploads/2010/06/logo.png")); | |
loadProgress = new ProgressBar(); | |
loadProgress.setPrefWidth(SPLASH_WIDTH - 20); | |
progressText = new Label("Loading hobbits with pie . . ."); | |
splashLayout = new VBox(); | |
splashLayout.getChildren().addAll(splash, loadProgress, progressText); | |
progressText.setAlignment(Pos.CENTER); | |
splashLayout.setStyle("-fx-padding: 5; -fx-background-color: cornsilk; -fx-border-width:5; -fx-border-color: linear-gradient(to bottom, chocolate, derive(chocolate, 50%));"); | |
splashLayout.setEffect(new DropShadow()); | |
} | |
@Override public void start(final Stage initStage) throws Exception { | |
showSplash(initStage); | |
showMainStage(); | |
webView.getEngine().documentProperty().addListener(new ChangeListener<Document>() { | |
@Override public void changed(ObservableValue<? extends Document> observableValue, Document document, Document document1) { | |
if (initStage.isShowing()) { | |
loadProgress.progressProperty().unbind(); | |
loadProgress.setProgress(1); | |
progressText.setText("All hobbits are full."); | |
mainStage.setIconified(false); | |
initStage.toFront(); | |
FadeTransition fadeSplash = new FadeTransition(Duration.seconds(1.2), splashLayout); | |
fadeSplash.setFromValue(1.0); | |
fadeSplash.setToValue(0.0); | |
fadeSplash.setOnFinished(new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent actionEvent) { | |
initStage.hide(); | |
} | |
}); | |
fadeSplash.play(); | |
} | |
} | |
}); | |
} | |
private void showMainStage() { | |
mainStage = new Stage(StageStyle.DECORATED); | |
mainStage.setTitle("FX Experience"); | |
mainStage.setIconified(true); | |
// create a WebView. | |
webView = new WebView(); | |
webView.getEngine().load("http://fxexperience.com/"); | |
loadProgress.progressProperty().bind(webView.getEngine().getLoadWorker().workDoneProperty().divide(100)); | |
// layout the scene. | |
Scene scene = new Scene(webView, 1000, 600); | |
webView.prefWidthProperty().bind(scene.widthProperty()); | |
webView.prefHeightProperty().bind(scene.heightProperty()); | |
mainStage.setScene(scene); | |
mainStage.show(); | |
} | |
private void showSplash(Stage initStage) { | |
Scene splashScene = new Scene(splashLayout); | |
initStage.initStyle(StageStyle.UNDECORATED); | |
final Rectangle2D bounds = Screen.getPrimary().getBounds(); | |
initStage.setScene(splashScene); | |
initStage.setX(bounds.getMinX() + bounds.getWidth() / 2 - SPLASH_WIDTH / 2); | |
initStage.setY(bounds.getMinY() + bounds.getHeight() / 2 - SPLASH_HEIGHT / 2); | |
initStage.show(); | |
} | |
} |
The splash image is loaded over http so that the demo can be a self-contained class, but usually you would load it from a local resource on the filesystem or in a jar.
Updated to add load progress monitoring to the splash page.
Added some progress text to the splash page.
Good example! thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://forums.oracle.com/forums/thread.jspa?threadID=233116