View RSS Feed

A JavaFX Blog

Where GUI events happen: the JavaFX Application Thread

Rate this Entry
by , 12-11-2016 at 06:20 PM (2809 Views)
When a JavaFX application is launched, the JavaFX framework creates a thread (generally referred to as the application thread, or the event thread) that handles the GUI of the application: for example, all GUI-generated events (mouse clicks, key presses, etc.) are processed in this thread, and any event handlers defined by the application programmer are executed in this thread. This is also the thread that manages all the updates to the application GUI, so in order to keep the interface responsive all programmer-defined event handlers must execute quickly; if any long-running tasks have to be executed as a result of a GUI-generated event (for example, connecting to a remote server on a network), a background thread should be created to handle these tasks.

The application can do any changes to the GUI (e.g. add a new widget, change a displayed picture, etc.) as long as the code is executed from within the application thread. So it is fine to make GUI changes from a GUI event handler, but not from within a background thread.

An Application subclass is instantiated and its init() method is called before the application thread is created, so no GUI-related operations can be done in the class constructor or in the init() method. On the other hand, the start() method is called from within the application thread, so this method can (and usually does) create the GUI for the application.

Java Code:
public class MyApplication extends Application {
  public void init() {
    // Not allowed: an exception will be thrown
    Stage myStage = new Stage();
  }
  ...
}

Submit "Where GUI events happen: the JavaFX Application Thread" to Facebook Submit "Where GUI events happen: the JavaFX Application Thread" to Digg Submit "Where GUI events happen: the JavaFX Application Thread" to del.icio.us Submit "Where GUI events happen: the JavaFX Application Thread" to StumbleUpon Submit "Where GUI events happen: the JavaFX Application Thread" to Google

Updated 12-11-2016 at 06:34 PM by JavaFX

Tags: None Add / Edit Tags
Categories
JavaFX Application , JavaFX Events

Comments