Open In App

JavaFX | Popup Class

Improve
Improve
Like Article
Like
Save
Share
Report

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:

Methods Explanation
getContent() The ObservableList of Nodes to be rendered on this Popup.
setAutoHide(boolean v) Sets the value of auto hide to the specified boolean value.
isShowing() Returns whether the popup is visible or not.

Below programs illustrate the use of Popup class:

  1. 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 stage
    import 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:


  2. 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() function
    import 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



Last Updated : 30 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads