Quick Contact

    Hibernate

    JavaFX Stage

    The JavaFX Stage is a desktop screen which displays JavaFX output to the user, also it is said that Stage is window. Inside a JavaFX Stage we are inserting a JavaFX element Scene which displays the content inside the window or we can say inside a Stage. When main method called from an application inside launch() method calls automatically start(). This start method has one argument that is Stage object, which creates primary stage object.

    This primary stage object creates a window to display user created functionality in the output. If your requirement is more than one stage then we can create as many Stage objects as we want. JavaFX Stage class available in javafx.stage.Stagepackage. So if we want to make use of Stage then we must import this package. It displays the output to the end user from a stage window.

    Constructors

    Given below are the constructors:

    • Stage():

      This Stage() constructor create a instance by new keyword without any arguments.

    • Stage(Node n):

      This Stage() constructor create a instance by new keyword with any JavaFX element as argument.

    Frequently used Methods

    Given below are the frequently used methods:

    1. show():

      The show() method will show the output.

    2. setScene(scene):

      The setScene() method set the Scene object.

    3. setTitle():

      The setTitle() sets the title of the application.

    4. setX(int value):

      The setX() method sets the position of the upper left corner of the application.

    5. setY(int value):

      The setY() method sets the position of the upper left corner of the application.

    6. setWidth(int width):

      The setWidth() method sets the width of stage window.

    7. setHeight(int height):

      The setHeight() method sets the height of stage window.

    8. showAndWait():

      The showAndWait() method will show the output but object will be still alive until stage is closed.

    9. initModality():

      The initModality must be done before making the stage visible.

    10. It has below constant values:

      • None:

        It is a default value.

      • WINDOW_MODAL:

        This value makes stage that blocks input events from all of windows from its owner and to its root object.

      • APPLICATION_MODAL:

        This value makes stage that blocks input events from all of windows from the same application.

    11. initOwner():

      The initOwner() method sets the owner of the application.

    12. initStyle():

      The initStyle() method sets the style of the stage window.

    It has below constant values:

    • Undecorated:

      This value gives without any decoration means white background.

    • Transparent:

      This value gives transparent background.

    • Unified:

      This value is not given any border between decoration area and main content area.

    • Utility:

      This value gives you minimal decorations.

    How does Stage work in JavaFX?

    • Accessing JavaFX features user defined class must extendsApplication class.
    • In JavaFX creating any JavaFX element is first step. ImageView, AnchorePane, ScrollPane, MenuBar etc.
    • This can instantiate by using new keyword.
    • Syntax:

      ImageViewimageView=new ImageView();
      ScrollPanescrollPane=new ScrollPane();
      AnchorPaneanchorPaneRef=new AnchorPane ();
      .
      .
      Etc.
      
    • Create VBox or any other display(like TilePane or HBox as per requirement) class to add the items is second step.
    • Syntax:

      VBoxvBox=new VBox (scrollPane or ImageView ,anchorPaneRef etc.); //Gives vertical box

    • Third step is creating scene for apply show method on to it.
    • Syntax:

      Scene screen = new Scene(vBox, length, width);

    • Adding Scene reference screen to the Stage object reference is fourth step. Adding output screen to Stage. We will get this stage object reference from start predefined JavaFX method.
    • Syntax:

      stage.setScene(screen);

    • Fifth step is showing output to the end user by applying show () method on the scene object.
    • Syntax:

      stage.show();

    Examples of JavaFX Stage

    Given below are the examples:

    Example #1

    Code:

    package com.stage;
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    public class StageDemo extends Application {
    @Override
    public void start(Stage outStage) throws Exception {
    // setting the title for our application
    outStage.setTitle("Stage Demo");
    // creating a group object for adding it to the scene
    Group group = new Group();
    // creating a scene object with width and height
    Scene sceneOut = new Scene(group, 520, 520);
    // setting AQUA color to the scene object
    sceneOut.setFill(Color.AQUA);
    // Adding scene object to the stage object
    outStage.setScene(sceneOut);
    // showing the output to the user
    outStage.show();
    }
    public static void main(String args[]) {
    // JVM calls start method automatically
    launch(args);
    }
    }
    

    Example #2

    Code:

    package com.stage;
    import javafx.application.Application;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    public class ModalityDemo extends Application {
    @Override
    public void start(Stage primaryStage) {
    // Adding a tile to the application
    primaryStage.setTitle("Modality Demo");
    //Creating another stage other than primary default stage
    Stagestage = new Stage();
    stage.setTitle("Secondary Stage");
    //Set the width and height of the other stage window
    stage.setWidth(400);
    stage.setHeight(400);
    //setting modality to APPLICATION_MODAL
    stage.initModality(Modality.APPLICATION_MODAL);
    // stage.initModality(Modality.WINDOW_MODAL);
    // stage.initModality(Modality.NONE);
    //Set the width and height of the primary stage window
    primaryStage.setWidth(500);
    primaryStage.setHeight(500);
    //It will show the output
    primaryStage.show();
    //It will show the output until stage object closed
    stage.showAndWait();
    }
    public static void main(String args[]) {
    // JVM calls start method automatically
    launch(args);
    }
    }
    

    Example #3

    Stage style with Label.

    Code:

    package com.stage;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.VBox;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    import javafx.stage.StageStyle;
    public class StageStyleDemo extends Application {
    @Override
    public void start(Stage primaryStage) {
    // Adding a tile to the application
    primaryStage.setTitle("Stage Style Demo");
    // It applies the styles to window
    // stage.initStyle(StageStyle.UNDECORATED);
    // stage.initStyle(StageStyle.TRANSPARENT);
    primaryStage.initStyle(StageStyle.UNIFIED);
    // stage.initStyle(StageStyle.UTILITY);
    // creating a label object
    Label label = new Label("\n\n\t\t\tHello, Hi I am Stage Demo Content");
    // creating vbox object
    VBoxvBox = new VBox(label);
    // creating a scene object
    Scene scene = new Scene(vBox,400,400);
    primaryStage.setScene(scene);
    // It will show the output
    primaryStage.show();
    }
    public static void main(String args[]) {
    // JVM calls start method automatically
    launch(args);
    }
    }
    

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.