Updated April 4, 2023
Introduction to JavaFX Popup
The JavaFX Popup is one of the class it is used for to creating the popup with no datas that is shown with the only alert message the class which is used for displaying the notifications by using some ui tag elements like buttons, labels and text boxes also the pop-up box has no alternatives like design the ui elements and aligning the borders, panels it’s not needed for ui related decorations, and we needed for to calling the method Popup() by using the Popup class, and it has some default methods for achieving this functionality that is needed for the project requirements.
Syntax:
In JavaFX, the ui based technology has some default classes, packages and methods. Likewise, the popup is one of the class, and it is called by using the popup() method with the help of its instance; by using this reference, the method is called wherever it’s needed on the project.
import javafx.*;
class classname extends Application
{
Access modifiers return type start()
{
Popup reference = new Popup(); // creating the popup reference by using the popup class
reference.default method();
----some java based logic codes----
}
public static void main(String args[])
{
//default method
lauch(args);
}
}
The above code is the basic syntax for creating and utilising the popup class in the JavaFX application. By using the popup class reference, we called the default methods and their behaviors.
How JavaFX Popup Function Works?
- In general, the popup class is one of the class and its method for functioning the windowing system it shows some popup boxes with similar to the alert boxes. If we clicked the popup boxes, the window resizing should be performed; that is, the windows boxes should be enabled or disabled because if we want to select or to click the other functionality of the application, it could not be worked until the popup operations should be ended on the user screen. So that’s the way we can be interacted and operated in the popup boxes; after that, only we will go into the other areas of the application.
- We can be used to multiple classes in the multiple set of packages in the JavaFX library; the popup class is used to create the reference of the class, and it’s been called wherever it’s needed in the project. The popup boxes cover other ui related areas like labels, text boxes, buttons, etc. these areas must ensure when the popup boxes should be configured and called in the same functions. Whenever the popup class created the instance, it calls the methods, and it is shown on the application window in JavaFX; it called it as Stage, so a new Stage is created when the popup reference is called in the application.
Constructors
We already know about the constructor because if we used popup class in the project, they needed or need for their behavior in other areas like sub classes, etc. So in that, we want to create the reference that is an object is needed for to calling the behavior in other project areas. We can also use the some inbuilt UI methods for calling those constructors by using both argument and non-argument type.
Code:
Access Modifier Popup() // classname
{
this.variable name1 = v1;
this.variable name2 = v2;
this.variable name3 = v3;
}
- Popup(): Constructor created without the parameters.
- Popup(Arguments): Constructor which has created with the single parameters or arguments.
- Popup(Arguments1, Arguments2): Constructor which has created with the multiple parameters or arguments.
The arguments should be any data types, Nodes etc.
Methods
The JavaFX package has a set of classes and methods each method will use wherever it needs in the fx classes. So that popup classes will be called each default methods of the JavaFX packages, especially in the stage, event, scene classes. Basically, it needs to start the start method by running the JavaFX programming codes.
Given below are some methods to handle this popup class in the UI.
- Access Modifier return type handle(): This method is used for handling the popup contents.
- Access Modifier return type getContent(): This method is used to get the contents from the user inputs.
- Access Modifier return type setAutoHide(): This method is used for hiding the popups in the application window.
- Access Modifier return type autoHideProperty(): This method also it’s a property for hiding the popups while the property is enabled or disable in the method.
- Access Modifier return type show(): This method is used for to show or display the popup contents on the user screen.
Likewise, the different methods based on the popup class handle the popup data contents with different application areas.
Examples of JavaFX Popup
Given below are the examples of JavaFX Popup:
Example #1
Code:
package com.first;
//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 Example extends Application {
public void start(Stage st)
{
st.setTitle("This is the First Example for popup class");
Button btn = new Button("Welcome User");
TilePane tp = new TilePane();
Label lb = new Label("Its a First popup example for users");
Popup pp = new Popup();
st.show();
lb.setStyle("background-color: green;");
pp.getContent().add(lb);
lb.setMinWidth(97);
lb.setMinHeight(63);
EventHandler<ActionEvent> en =
new EventHandler<ActionEvent>() {
public void handle(ActionEvent et)
{
if (!pp.isShowing())
pp.show(st);
else
pp.hide();
System.out.println("The popup is hidden");
}
};
btn.setOnAction(en);
tp.getChildren().add(btn);
Scene sc = new Scene(tp, 303, 302);
st.setScene(sc);
st.show();
}
public static void main(String args[])
{
System.out.println("Welcome to My Domains");
launch(args);
}
}
Output:
Example #2
Code:
package com.first;
//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;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.*;
import javafx.scene.image.*;
public class Example extends Application {
public void start(Stage st1) throws FileNotFoundException
{
st1.setTitle("This is the second Example for popup class");
FileInputStream f = new FileInputStream("C:\\Users\\Kripya-PC\\Downloads\\T16 Fresh+ Grapefruit Mint ITA\\T16 Fresh+ Grapefruit Mint ITA.png");
Image im = new Image(f);
ImageView iv = new ImageView(im);
Button btn1 = new Button("Welcome again Usres",iv);
TilePane tp1 = new TilePane();
Label lb1 = new Label("Its a second popup example for users");
Popup p1 = new Popup();
st1.show();
p1.getContent().add(lb1);
lb1.setMinWidth(103);
lb1.setMinHeight(174);
EventHandler<ActionEvent> en1 =
new EventHandler<ActionEvent>() {
public void handle(ActionEvent et1)
{
if (!p1.isShowing())
p1.show(st1);
else
p1.hide();
System.out.println("The popup is hidden for second example");
}
};
btn1.setOnAction(en1);
tp1.getChildren().add(btn1);
Scene sc1 = new Scene(tp1, 401, 204);
st1.setScene(sc1);
st1.show();
}
public static void main(String args[])
{
System.out.println("Welcome to My Domains");
launch(args);
}
}
Output:
Example #3
Code:
public class Example extends Application {
public void start(Stage st2)
{
Popup po = new Popup();
po.setX(205);
po.setY(304);
po.getContent().addAll(new Circle(35, 45, 64, Color.RED));
Button btn1 = new Button("Welcome User");
btn1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent en) {
po.show(st2);
}
});
Button btn2 = new Button("Thanks User");
btn2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent en) {
po.hide();
} });
HBox hb = new HBox(17);
hb.setStyle("-fx-background-color: violet; -fx-padding: 13px;");
hb.getChildren().addAll(btn1, btn2);
st2.setScene(new Scene(hb));
st2.show();
}
public static void main(String args[])
{
System.out.println("Welcome to My Domains");
launch(args);
}
}
Output:
Conclusion
In conclusion, the JavaFX is one of the finest parts of the java technology. It is widely used in the UI-related things in the application. The popup is one of the classes used to alert or notifies the ui elements; it depends upon the user requirement in the project.
Recommended Articles
This is a guide to JavaFX Popup. Here we discuss the introduction, how the JavaFX popup function works? constructors, methods and examples. You may also have a look at the following articles to learn more –