JavaFX | Popup Class - GeeksforGeeks

30 Aug, 2018
Popup class is a part of JavaFX. Popup class creates a popup with no content, a null fill and is transparent. Popup class is used to display a notification, buttons, or a drop-down menu and so forth. The popup has no decorations. It essentially acts as a specialized scene/window which has no decorations.
Constructor of the class:
Popup(): Creates an object of Popup class.
Commonly Used Methods:
Below programs illustrate the use of Popup class:
Java program to create a popup and add it to the stage: In this program we create a Popup named popup. The popup contains a Label named label. We also create a Button named button and add event handler to it and then display the popup if it is hidden and hide it if it is already visible. The button is added to the TilePane and the TilePane is added to the scene, and the scene is added to the stage. The show function is called to display the results. The background color of the label is set using the setStyle() function, and the label size is set using setMinHeight(), setMinWidth() function. The hide() and show() function is used to hide or show the popup.// Java program to create a popup and // add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import javafx.stage.Popup;   public class Popup_1 extends Application {       // launch the application    public void start(Stage stage)    {           // set title for the stage        stage.setTitle("Creating popup");           // create a button        Button button = new Button("button");           // create a tile pane        TilePane tilepane = new TilePane();           // create a label        Label label = new Label("This is a Popup");           // create a popup        Popup popup = new Popup();           // set background        label.setStyle(" -fx-background-color: white;");           // add the label        popup.getContent().add(label);           // set size of label        label.setMinWidth(80);        label.setMinHeight(50);           // action event        EventHandler<ActionEvent> event =         new EventHandler<ActionEvent>() {               public void handle(ActionEvent e)            {                if (!popup.isShowing())                    popup.show(stage);                else                    popup.hide();            }        };           // when button is pressed        button.setOnAction(event);           // add button        tilepane.getChildren().add(button);           // create a scene        Scene scene = new Scene(tilepane, 200, 200);           // set the scene        stage.setScene(scene);           stage.show();    }       // Main Method    public static void main(String args[])    {           // launch the application        launch(args);    }}Output:Video Playerhttps://media.geeksforgeeks.org/wp-content/uploads/Popup_1.mp400:0000:0000:14Use Up/Down Arrow keys to increase or decrease volume.Java Program to create a popup and add it to the stage and make the popup hide automatically when it loses focus using the setAutoHide() function: In this program we create a Popup named popup. The popup contains a Label named label. We also create a Button named button and add event handler to it, to display the popup if it is hidden. The button is added to the TilePane and the TilePane is added to the scene, and the scene is added to the stage. The show function is called to display the results. The popup will automatically hide when it loses focus, we will apply this feature to the popup using the setAutoHide() function.The background color of the label is set using the setStyle() function, and the label size is set using setMinHeight(), setMinWidth() function. The hide() and show() function is used to hide or show the popup.// Java Program to create a popup and add// it to the stage and make the popup hide// automatically when it loses focus using// the setAutoHide() functionimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import javafx.stage.Popup;  public class popup_2 extends Application {      // launch the application    public void start(Stage stage)    {          // set title for the stage        stage.setTitle("Creating Popup");          // create a button        Button button = new Button("popup");          // create a tile pane        TilePane tilepane = new TilePane();          // create a label        Label label = new Label("This is a Popup");          // create a popup        Popup popup = new Popup();          // set background        label.setStyle(" -fx-background-color: white;");          // add the label        popup.getContent().add(label);          // set size of label        label.setMinWidth(80);        label.setMinHeight(50);          // set auto hide        popup.setAutoHide(true);          // action event        EventHandler<ActionEvent> event =         new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                if (!popup.isShowing())                    popup.show(stage);            }        };          // when button is pressed        button.setOnAction(event);          // add button        tilepane.getChildren().add(button);          // create a scene        Scene scene = new Scene(tilepane, 200, 200);          // set the scene        stage.setScene(scene);          stage.show();    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Video Playerhttps://media.geeksforgeeks.org/wp-content/uploads/Popup_2.mp400:0000:0000:22Use Up/Down Arrow keys to increase or decrease volume.
Java program to create a popup and add it to the stage: In this program we create a Popup named popup. The popup contains a Label named label. We also create a Button named button and add event handler to it and then display the popup if it is hidden and hide it if it is already visible. The button is added to the TilePane and the TilePane is added to the scene, and the scene is added to the stage. The show function is called to display the results. The background color of the label is set using the setStyle() function, and the label size is set using setMinHeight(), setMinWidth() function. The hide() and show() function is used to hide or show the popup.// Java program to create a popup and // add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import javafx.stage.Popup;   public class Popup_1 extends Application {       // launch the application    public void start(Stage stage)    {           // set title for the stage        stage.setTitle("Creating popup");           // create a button        Button button = new Button("button");           // create a tile pane        TilePane tilepane = new TilePane();           // create a label        Label label = new Label("This is a Popup");           // create a popup        Popup popup = new Popup();           // set background        label.setStyle(" -fx-background-color: white;");           // add the label        popup.getContent().add(label);           // set size of label        label.setMinWidth(80);        label.setMinHeight(50);           // action event        EventHandler<ActionEvent> event =         new EventHandler<ActionEvent>() {               public void handle(ActionEvent e)            {                if (!popup.isShowing())                    popup.show(stage);                else                    popup.hide();            }        };           // when button is pressed        button.setOnAction(event);           // add button        tilepane.getChildren().add(button);           // create a scene        Scene scene = new Scene(tilepane, 200, 200);           // set the scene        stage.setScene(scene);           stage.show();    }       // Main Method    public static void main(String args[])    {           // launch the application        launch(args);    }}Output:Video Playerhttps://media.geeksforgeeks.org/wp-content/uploads/Popup_1.mp400:0000:0000:14Use Up/Down Arrow keys to increase or decrease volume.
// Java program to create a popup and // add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import javafx.stage.Popup;   public class Popup_1 extends Application {       // launch the application    public void start(Stage stage)    {           // set title for the stage        stage.setTitle("Creating popup");           // create a button        Button button = new Button("button");           // create a tile pane        TilePane tilepane = new TilePane();           // create a label        Label label = new Label("This is a Popup");           // create a popup        Popup popup = new Popup();           // set background        label.setStyle(" -fx-background-color: white;");           // add the label        popup.getContent().add(label);           // set size of label        label.setMinWidth(80);        label.setMinHeight(50);           // action event        EventHandler<ActionEvent> event =         new EventHandler<ActionEvent>() {               public void handle(ActionEvent e)            {                if (!popup.isShowing())                    popup.show(stage);                else                    popup.hide();            }        };           // when button is pressed        button.setOnAction(event);           // add button        tilepane.getChildren().add(button);           // create a scene        Scene scene = new Scene(tilepane, 200, 200);           // set the scene        stage.setScene(scene);           stage.show();    }       // Main Method    public static void main(String args[])    {           // launch the application        launch(args);    }}
Output:
Java Program to create a popup and add it to the stage and make the popup hide automatically when it loses focus using the setAutoHide() function: In this program we create a Popup named popup. The popup contains a Label named label. We also create a Button named button and add event handler to it, to display the popup if it is hidden. The button is added to the TilePane and the TilePane is added to the scene, and the scene is added to the stage. The show function is called to display the results. The popup will automatically hide when it loses focus, we will apply this feature to the popup using the setAutoHide() function.The background color of the label is set using the setStyle() function, and the label size is set using setMinHeight(), setMinWidth() function. The hide() and show() function is used to hide or show the popup.// Java Program to create a popup and add// it to the stage and make the popup hide// automatically when it loses focus using// the setAutoHide() functionimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import javafx.stage.Popup;  public class popup_2 extends Application {      // launch the application    public void start(Stage stage)    {          // set title for the stage        stage.setTitle("Creating Popup");          // create a button        Button button = new Button("popup");          // create a tile pane        TilePane tilepane = new TilePane();          // create a label        Label label = new Label("This is a Popup");          // create a popup        Popup popup = new Popup();          // set background        label.setStyle(" -fx-background-color: white;");          // add the label        popup.getContent().add(label);          // set size of label        label.setMinWidth(80);        label.setMinHeight(50);          // set auto hide        popup.setAutoHide(true);          // action event        EventHandler<ActionEvent> event =         new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                if (!popup.isShowing())                    popup.show(stage);            }        };          // when button is pressed        button.setOnAction(event);          // add button        tilepane.getChildren().add(button);          // create a scene        Scene scene = new Scene(tilepane, 200, 200);          // set the scene        stage.setScene(scene);          stage.show();    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Video Playerhttps://media.geeksforgeeks.org/wp-content/uploads/Popup_2.mp400:0000:0000:22Use Up/Down Arrow keys to increase or decrease volume.
// Java Program to create a popup and add// it to the stage and make the popup hide// automatically when it loses focus using// the setAutoHide() functionimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.control.Label;import javafx.stage.Stage;import javafx.stage.Popup;  public class popup_2 extends Application {      // launch the application    public void start(Stage stage)    {          // set title for the stage        stage.setTitle("Creating Popup");          // create a button        Button button = new Button("popup");          // create a tile pane        TilePane tilepane = new TilePane();          // create a label        Label label = new Label("This is a Popup");          // create a popup        Popup popup = new Popup();          // set background        label.setStyle(" -fx-background-color: white;");          // add the label        popup.getContent().add(label);          // set size of label        label.setMinWidth(80);        label.setMinHeight(50);          // set auto hide        popup.setAutoHide(true);          // action event        EventHandler<ActionEvent> event =         new EventHandler<ActionEvent>() {            public void handle(ActionEvent e)            {                if (!popup.isShowing())                    popup.show(stage);            }        };          // when button is pressed        button.setOnAction(event);          // add button        tilepane.getChildren().add(button);          // create a scene        Scene scene = new Scene(tilepane, 200, 200);          // set the scene        stage.setScene(scene);          stage.show();    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Popup.html
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
ArrayList in Java
Stack Class in Java
Singleton Class in Java
Set in Java
Multithreading in Java
Collections in Java
Queue Interface In Java
LinkedList in Java