Package javafx.stage

Class Stage

All Implemented Interfaces:
HasHeightProperty, HasSceneProperty, HasTitleProperty, HasWidthProperty, EventTarget

public class Stage extends Window implements HasTitleProperty
The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application.

Stage objects must be constructed and modified on the JavaFX Application Thread.

Many of the Stage properties are read only because they can be changed externally by the underlying platform and therefore must not be bindable.

Style

A stage has one of the following styles:

The style must be initialized before the stage is made visible.

On some platforms decorations might not be available. For example, on some mobile or embedded devices. In these cases a request for a DECORATED or UTILITY window will be accepted, but no decorations will be shown.

Owner

A stage can optionally have an owner Window. When a window is a stage's owner, it is said to be the parent of that stage.

Owned Stages are tied to the parent Window. An owned stage will always be on top of its parent window. When a parent window is closed or iconified, then all owned windows will be affected as well. Owned Stages cannot be independantly iconified.

The owner must be initialized before the stage is made visible.

Modality

A stage has one of the following modalities:

  • Modality.NONE - a stage that does not block any other window.
  • Modality.WINDOW_MODAL - a stage that blocks input events from being delivered to all windows from its owner (parent) to its root. Its root is the closest ancestor window without an owner.
  • Modality.APPLICATION_MODAL - a stage that blocks input events from being delivered to all windows from the same application, except for those from its child hierarchy.

When a window is blocked by a modal stage its Z-order relative to its ancestors is preserved, and it receives no input events and no window activation events, but continues to animate and render normally. Note that showing a modal stage does not necessarily block the caller. The show() method returns immediately regardless of the modality of the stage. Use the showAndWait() method if you need to block the caller until the modal stage is hidden (closed). The modality must be initialized before the stage is made visible.

Example:


 import javafx.application.Application;
 import javafx.scene.Group;
 import javafx.scene.Scene;
 import javafx.scene.text.Font;
 import javafx.scene.text.Text;
 import javafx.stage.Stage;

 public class HelloWorld extends Application {

 @Override public void start(Stage stage) {
 Text text = new Text(10, 40, "Hello World!");
 text.setFont(new Font(40));
 Scene scene = new Scene(new Group(text));

 stage.setTitle("Welcome to JavaFX!");
 stage.setScene(scene);
 stage.sizeToScene();
 stage.show();
 }

 public static void main(String[] args) {
 Application.launch(args);
 }
 }

 

produces the following on Windows:

produces the following on Mac OSX:

produces the following on Linux:

Since:
JavaFX 2.0
  • Property Details

    • title

      public Property<String> titleProperty
      Specified by:
      titleProperty in interface HasTitleProperty
      Returns:
      the title property
      See Also:
    • resizable

      public final Property<Boolean> resizableProperty
      Defines whether the Stage is resizable or not by the user. Programatically you may still change the size of the Stage. This is a hint which allows the implementation to optionally make the Stage resizable by the user.

      Warning: Since 8.0 the property cannot be bound and will throw RuntimeException on an attempt to do so. This is because the setting of resizable is asynchronous on some systems or generally might be set by the system / window manager.
      Bidirectional binds are still allowed, as they don't block setting of the property by the system.

      Default value:
      true
      See Also:
    • fullScreen

      public final ReadOnlyBooleanProperty fullScreenProperty
      See Also:
  • Constructor Details

    • Stage

      public Stage()
      Creates a new instance of decorated Stage.
      Throws:
      IllegalStateException - if this constructor is called on a thread other than the JavaFX Application Thread.
    • Stage

      public Stage(StageStyle style)
      Creates a new instance of Stage.
      Parameters:
      style - The style of the Stage
      Throws:
      IllegalStateException - if this constructor is called on a thread other than the JavaFX Application Thread.
  • Method Details

    • createPeer

      protected StagePeer createPeer()
      Overrides:
      createPeer in class Window
    • titleProperty

      public Property<String> titleProperty()
      Specified by:
      titleProperty in interface HasTitleProperty
      Returns:
      the title property
      See Also:
    • setScene

      public final void setScene(Scene value)
      Specify the scene to be used on this stage.
      Specified by:
      setScene in interface HasSceneProperty
      Parameters:
      value - the value for the scene property
      See Also:
    • show

      public final void show()
      Description copied from class: Window
      Attempts to show this Window by setting visibility to true
      Overrides:
      show in class Window
    • showAndWait

      public void showAndWait()
      Shows this stage and waits for it to be hidden (closed) before returning to the caller. This method temporarily blocks processing of the current event, and starts a nested event loop to handle other events. This method must be called on the FX Application thread.

      A Stage is hidden (closed) by one of the following means:

      • the application calls the Window.hide() or close() method on this stage
      • this stage has a non-null owner window, and its owner is closed
      • the user closes the window via the window system (for example, by pressing the close button in the window decoration)

      After the Stage is hidden, and the application has returned from the event handler to the event loop, the nested event loop terminates and this method returns to the caller.

      For example, consider the following sequence of operations for different event handlers, assumed to execute in the order shown below:

      void evtHander1(...) {
           stage1.showAndWait();
           doSomethingAfterStage1Closed(...)
       }
      
       void evtHander2(...) {
           stage1.hide();
           doSomethingElseHere(...)
       }
      evtHandler1 will block at the call to showAndWait. It will resume execution after stage1 is hidden and the current event handler, in this case evtHandler2, returns to the event loop. This means that doSomethingElseHere will execute before doSomethingAfterStage1Closed.

      More than one stage may be shown with showAndWait. Each call will start a new nested event loop. The stages may be hidden in any order, but a particular nested event loop (and thus the showAndWait method for the associated stage) will only terminate after all inner event loops have also terminated.

      For example, consider the following sequence of operations for different event handlers, assumed to execute in the order shown below:

        void evtHander1() {
             stage1.showAndWait();
             doSomethingAfterStage1Closed(...)
         }
        
         void evtHander2() {
             stage2.showAndWait();
             doSomethingAfterStage2Closed(...)
         }
        
         void evtHander3() {
             stage1.hide();
             doSomethingElseHere(...)
         }
        
         void evtHander4() {
             stage2.hide();
             doSomethingElseHereToo(...)
         }
      evtHandler1 will block at the call to stage1.showAndWait, starting up a nested event loop just like in the previous example. evtHandler2 will then block at the call to stage2.showAndWait, starting up another (inner) nested event loop. The first call to stage1.showAndWait will resume execution after stage1 is hidden, but only after the inner nested event loop started by stage2.showAndWait has terminated. This means that the call to stage1.showAndWait won't return until after evtHandler2 has returned. The order of execution is: stage1.showAndWait, stage2.showAndWait, stage1.hide, doSomethingElseHere, stage2.hide, doSomethingElseHereToo, doSomethingAfterStage2Closed, doSomethingAfterStage1Closed.

      This method must not be called on the primary stage or on a stage that is already visible. Additionally, it must either be called from an input event handler or from the run method of a Runnable passed to Platform.runLater. It must not be called during animation or layout processing.

      Throws:
      IllegalStateException - if this method is called on a thread other than the JavaFX Application Thread.
      IllegalStateException - if this method is called during animation or layout processing.
      IllegalStateException - if this method is called on the primary stage.
      IllegalStateException - if this stage is already showing.
      Since:
      JavaFX 2.2
    • initStyle

      public final void initStyle(StageStyle style)
      Specifies the style for this stage. This must be done prior to making the stage visible. The style is one of: StageStyle.DECORATED, StageStyle.UNDECORATED, StageStyle.TRANSPARENT, or StageStyle.UTILITY.
      Default value:
      StageStyle.DECORATED
      Parameters:
      style - the style for this stage.
      Throws:
      IllegalStateException - if this property is set after the stage has ever been made visible.
    • getStyle

      public final StageStyle getStyle()
      Retrieves the style attribute for this stage.
      Returns:
      the stage style.
    • initModality

      public final void initModality(Modality modality)
      Specifies the modality for this stage. This must be done prior to making the stage visible. The modality is one of: Modality.NONE, Modality.WINDOW_MODAL, or Modality.APPLICATION_MODAL.
      Default value:
      Modality.NONE
      Parameters:
      modality - the modality for this stage.
      Throws:
      IllegalStateException - if this property is set after the stage has ever been made visible.
      IllegalStateException - if this stage is the primary stage.
    • getModality

      public final Modality getModality()
      Retrieves the modality attribute for this stage.
      Returns:
      the modality.
    • initOwner

      public final void initOwner(Window owner)
      Specifies the owner Window for this stage, or null for a top-level, unowned stage. This must be done prior to making the stage visible.
      Default value:
      null
      Parameters:
      owner - the owner for this stage.
      Throws:
      IllegalStateException - if this property is set after the stage has ever been made visible.
      IllegalStateException - if this stage is the primary stage.
    • getOwner

      public final Window getOwner()
      Retrieves the owner Window for this stage, or null for an unowned stage.
      Returns:
      the owner Window.
    • setResizable

      public final void setResizable(boolean value)
      Sets the value of the resizable property.
      Property description:
      Defines whether the Stage is resizable or not by the user. Programatically you may still change the size of the Stage. This is a hint which allows the implementation to optionally make the Stage resizable by the user.

      Warning: Since 8.0 the property cannot be bound and will throw RuntimeException on an attempt to do so. This is because the setting of resizable is asynchronous on some systems or generally might be set by the system / window manager.
      Bidirectional binds are still allowed, as they don't block setting of the property by the system.

      Default value:
      true
      Parameters:
      value - the value for the resizable property
      See Also:
    • isResizable

      public final boolean isResizable()
    • resizableProperty

      public final Property<Boolean> resizableProperty()
      Defines whether the Stage is resizable or not by the user. Programatically you may still change the size of the Stage. This is a hint which allows the implementation to optionally make the Stage resizable by the user.

      Warning: Since 8.0 the property cannot be bound and will throw RuntimeException on an attempt to do so. This is because the setting of resizable is asynchronous on some systems or generally might be set by the system / window manager.
      Bidirectional binds are still allowed, as they don't block setting of the property by the system.

      Default value:
      true
      Returns:
      the resizable property
      See Also:
    • close

      public void close()
      Closes this Stage. This call is equivalent to hide().
    • fullScreenPropertyImpl

      public BooleanProperty fullScreenPropertyImpl()
    • fullScreenProperty

      public final ReadOnlyBooleanProperty fullScreenProperty()
      Returns:
      the fullScreen property
      See Also:
    • setFullScreen

      public final void setFullScreen(boolean value)
      Sets the value of the fullScreen property.
      Property description:
      Parameters:
      value - the value for the fullScreen property
      See Also:
    • isFullScreen

      public final boolean isFullScreen()
      Gets the value of the fullScreen property.
      Property description:
      Returns:
      the value of the fullScreen property
      See Also:
    • setFullScreenExitHint

      public void setFullScreenExitHint(String hint)
    • setMinWidth

      public void setMinWidth(double minWidth)
    • setMinHeight

      public void setMinHeight(double minHeight)
    • getIcons

      public final ObservableList<Image> getIcons()