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 application   Âpublicvoidstart(Stage stage)   Â{ Â       Â// set title for the stage       Âstage.setTitle("Creating popup"); Â       Â// create a button       ÂButton button =newButton("button"); Â       Â// create a tile pane       ÂTilePane tilepane =newTilePane(); Â       Â// create a label       ÂLabel label =newLabel("This is a Popup"); Â       Â// create a popup       ÂPopup popup =newPopup(); Â       Â// 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 =       ÂnewEventHandler<ActionEvent>() { Â           Âpublicvoidhandle(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 =newScene(tilepane,200,200); Â       Â// set the scene       Âstage.setScene(scene); Â       Âstage.show();   Â} Â   Â// Main Method   Âpublicstaticvoidmain(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() 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 application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("Creating Popup");       Â// create a button       ÂButton button =newButton("popup");       Â// create a tile pane       ÂTilePane tilepane =newTilePane();       Â// create a label       ÂLabel label =newLabel("This is a Popup");       Â// create a popup       ÂPopup popup =newPopup();       Â// 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 =       ÂnewEventHandler<ActionEvent>() {           Âpublicvoidhandle(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 =newScene(tilepane,200,200);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Â// Main Method   Âpublicstaticvoidmain(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
