public abstract class Application extends Object
Life-cycle
The entry point for JavaFX applications is the Application class. The JavaFX runtime does the following, in order, whenever an application is launched:
init()
methodstart(javafx.stage.Stage)
methodPlatform.exit()
implicitExit
attribute on Platform
is truestop()
methodNote that the start
method is abstract and must be overridden.
The init
and stop
methods have concrete implementations
that do nothing.
Calling Platform.exit()
is the preferred way to explicitly terminate
a JavaFX Application. Directly calling System.exit(int)
is
an acceptable alternative, but doesn't allow the Application stop()
method to run.
A JavaFX Application should not attempt to use JavaFX after the
FX toolkit has terminated or from a ShutdownHook, that is, after the
stop()
method returns or System.exit(int)
is called.
Parameters
Application parameters are available by calling the getParameters()
method from the init()
method, or any time after the init
method has been called.
Threading
JavaFX creates an application thread for running the application start
method, processing input events, and running animation timelines. Creation
of JavaFX Scene
and Stage
objects as well as modification of
scene graph operations to live objects (those objects already
attached to a scene) must be done on the JavaFX application thread.
The Java launcher loads and initializes the specified Application class on the JavaFX Application Thread. If there is no main method in the Application class, or if the main method calls Application.launch(), then an instance of the Application is then constructed on the JavaFX Application Thread.
The init
method is called on the launcher thread, not on the
JavaFX Application Thread.
This means that an application must not construct a Scene
or a Stage
in the init
method.
An application may construct other JavaFX objects in the init
method.
All the unhandled exceptions on the JavaFX application thread that occur during
event dispatching, running animation timelines, or any other code, are forwarded
to the thread's uncaught
exception handler
.
Example
The following example will illustrate a simple JavaFX application.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class MyApp extends Application {
public void start(Stage stage) {
Circle circ = new Circle(40, 40, 30);
Group root = new Group(circ);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
}
The above example will produce the following:
Modifier and Type | Class and Description |
---|---|
static class |
Application.Parameters
Encapsulates the set of parameters for an application.
|
Modifier and Type | Field and Description |
---|---|
static String |
STYLESHEET_CASPIAN
Constant for user agent stylesheet for the "Caspian" theme.
|
static String |
STYLESHEET_MODENA
Constant for user agent stylesheet for the "Modena" theme.
|
Constructor and Description |
---|
Application()
Constructs a new
Application instance. |
Modifier and Type | Method and Description |
---|---|
HostServices |
getHostServices()
Gets the HostServices provider for this application.
|
Application.Parameters |
getParameters()
Retrieves the parameters for this Application, including any arguments
passed on the command line and any parameters specified in a JNLP file
for an applet or WebStart application.
|
static String |
getUserAgentStylesheet()
Get the user agent stylesheet used by the whole application.
|
void |
init()
The application initialization method.
|
static void |
launch(Class<? extends Application> appClass,
String... args)
Launch a standalone application.
|
static void |
launch(String... args)
Launch a standalone application.
|
void |
notifyPreloader(Preloader.PreloaderNotification info)
Notifies the preloader with an application-generated notification.
|
static void |
setUserAgentStylesheet(String url)
Set the user agent stylesheet used by the whole application.
|
abstract void |
start(Stage primaryStage)
The main entry point for all JavaFX applications.
|
void |
stop()
This method is called when the application should stop, and provides a
convenient place to prepare for application exit and destroy resources.
|
public static final String STYLESHEET_CASPIAN
public static final String STYLESHEET_MODENA
public static void launch(Class<? extends Application> appClass, String... args)
The launch method does not return until the application has exited, either via a call to Platform.exit or all of the application windows have been closed.
Typical usage is:
public static void main(String[] args) { Application.launch(MyApp.class, args); }where
MyApp
is a subclass of Application.appClass
- the application class that is constructed and executed
by the launcher.args
- the command line arguments passed to the application.
An application may get these parameters using the
getParameters()
method.IllegalStateException
- if this method is called more than once.IllegalArgumentException
- if appClass
is not a
subclass of Application
.public static void launch(String... args)
The launch method does not return until the application has exited, either via a call to Platform.exit or all of the application windows have been closed.
Typical usage is:
public static void main(String[] args) { Application.launch(args); }
args
- the command line arguments passed to the application.
An application may get these parameters using the
getParameters()
method.IllegalStateException
- if this method is called more than once.public void init() throws Exception
The implementation of this method provided by the Application class does nothing.
NOTE: This method is not called on the JavaFX Application Thread. An application must not construct a Scene or a Stage in this method. An application may construct other JavaFX objects in this method.
Exception
public abstract void start(Stage primaryStage) throws Exception
NOTE: This method is called on the JavaFX Application Thread.
primaryStage
- the primary stage for this application, onto which
the application scene can be set. The primary stage will be embedded in
the browser if the application was launched as an applet.
Applications may create other stages, if needed, but they will not be
primary stages and will not be embedded in the browser.Exception
public void stop() throws Exception
The implementation of this method provided by the Application class does nothing.
NOTE: This method is called on the JavaFX Application Thread.
Exception
public final HostServices getHostServices()
public final Application.Parameters getParameters()
NOTE: this method should not be called from the Application constructor, as it will return null. It may be called in the init() method or any time after that.
public final void notifyPreloader(Preloader.PreloaderNotification info)
Preloader.handleApplicationNotification
method.
This is primarily useful for cases where an application wants the
preloader to show progress during a long application initialization
step.
NOTE: the notification will be delivered only to the preloader's
handleApplicationNotification() method; this means, for example, that
if this method is called with a ProgressNotification, that notification
will not be delivered to the Preloader.handleProgressNotification
method.
info
- the application-generated preloader notificationpublic static String getUserAgentStylesheet()
NOTE: This method must be called on the JavaFX Application Thread.
public static void setUserAgentStylesheet(String url)
-Djavafx.userAgentStylesheetUrl=[URL]
Setting it on the command line overrides anything set using this method
in code.
NOTE: This method must be called on the JavaFX Application Thread.
url
- The URL to the stylesheet as a String.Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2008, 2017, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.