How to Create a Pop Up Window in JavaFX

Java


In this article, we show how to create a pop up window in JavaFX.

A pop up window functions pretty much just like an alert box. When clicked, the window appears. If you then try to click on the main window before the pop up appeared, you won't be able to. You first have to deal with the pop up window before you can interact back with the main window.

We do so we actually use 2 separate Java files.

One file represents the main window. The other file represents the pop up window.

Java Pop Up Window File

So first, we'll write the code for the pop window.

This is shown below.



So being that we are using multiple classes, we must contain a package statement in the classes. The package in this example is called firstgui.

We then import all the packages we need into our program so that it works.

We then create our class Popup.

In this class, we add a method named display().

In this method, we create how the window will look like.

The method is static so that we can just call the class in the main program instead of having to create an instance of this class. Therefore, the method is static. We will call it in the main program using the class name (Popup) followed by the method, display().

The first thing we do in this method is create a new Stage. Since this pop up window is a new window separate from the main window, we must create a new window. In JavaFX, a new a stage represents a window. So we have to create a new stage in order to get a new window.

So we create a stage named popupwindow. This represents the pop up window.

The line, popupwindow.initModality(Modality.APPLICATION_MODAL);, defines a window that blocks events from being delivered to any other application window. So once this pop up window appears, you cannot interact with any other window, including the main program window, until you either do what's required by the pop up window or close it out.

We set the title of the pop up window to, "This is a pop up window".

We then create a label, label1, to "Pop up window now displayed".

We then add a button to this pop up window named button 1, which has a text value of "Close this pop up window".

To this button, we add a close() function, so that when it is clicked, it closes.

Like all windows, we have to create a layout. In this case, we use VBox, so that the elements are vertically stacked. HBox is another layout format, which stacks elements horizontally. And StackPane is another layout that can be used. Passing the value, 10, into VBox just means that we want the vertical spacing in between elements to be 10 pixels.

We then add the label and the button to the layout.

We use the setAlignment() function to center the elements on the layout.

We then create the scene for the stage. Every stage must have a scene.

We do this by creating an instance of the Scene class. The object is named scene1. We pass into this object the layout, along with the width and height of the window.

We then use the showAndWait() function to show the window and what this function does is it keeps the window up until it is either hidden or closed out from. So we show this window and then wait until it is closed out.

So this is all that is required to create the pop up window in Java.

Now we show how to tie this in with the main program (the main window) so that we can show this pop up window when a user clicks on a button in the main window.

Main Window Class

The code for the main window is shown below.





So going really quick through this, we once again import the packages we need for the program.

We create a public class that extends the Application class.

We then override the start() method in the Application class.

We set the title to "JavaFX GUI".

We then create a button named buttonmain. The text of this button is set to, "Click to go to pop up window"

The next line is where we get the functionality to show the pop up window we've created.

We use the setOnAction() function to add event handling to the button in the main window. When the buton is clicked, the pop up window is shown. The e parameter is that when the button is clicked, the display() method is called from the Popup class. Since the method is a static method, it is called by taking the class name (Popup) and following it with a dot and the method name.

We then create a layout from the StackPane class.

We then add the button to the layout.

We then create the scene, adding the layout and the width and height of the window.

We then set the scene to this stage, primaryStage.

We then show the window.

The main method executes this program by launching it.

So when we run this code, we get the main window with the button that states, "Click to go to pop up window". When this button is clicked on, a pop up window opens that has the label, "Pop up window now displayed". This window also has a button that has the text, "Close this pop up window".

And this is all that is required to create a pop up window in JavaFX.


Related Resources

How to Add a Title to a Window Using JavaFX




HTML Comment Box is loading comments...