JavaFX | Popup Class

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:
- 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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;importjavafx.stage.Popup;publicclassPopup_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("Creating popup");// create a buttonButton button =newButton("button");// create a tile paneTilePane tilepane =newTilePane();// create a labelLabel label =newLabel("This is a Popup");// create a popupPopup popup =newPopup();// set backgroundlabel.setStyle(" -fx-background-color: white;");// add the labelpopup.getContent().add(label);// set size of labellabel.setMinWidth(80);label.setMinHeight(50);// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){if(!popup.isShowing())popup.show(stage);elsepopup.hide();}};// when button is pressedbutton.setOnAction(event);// add buttontilepane.getChildren().add(button);// create a sceneScene scene =newScene(tilepane,200,200);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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() functionimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;importjavafx.stage.Popup;publicclasspopup_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("Creating Popup");// create a buttonButton button =newButton("popup");// create a tile paneTilePane tilepane =newTilePane();// create a labelLabel label =newLabel("This is a Popup");// create a popupPopup popup =newPopup();// set backgroundlabel.setStyle(" -fx-background-color: white;");// add the labelpopup.getContent().add(label);// set size of labellabel.setMinWidth(80);label.setMinHeight(50);// set auto hidepopup.setAutoHide(true);// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){if(!popup.isShowing())popup.show(stage);}};// when button is pressedbutton.setOnAction(event);// add buttontilepane.getChildren().add(button);// create a sceneScene scene =newScene(tilepane,200,200);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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



