How to Communicate Between Windows in JavaFX 


               


How to Communicate Between Windows in JavaFX

Java


In this article, we show how to communicate between windows in JavaFX.

Many times in advanced applications, if you have multiple windows in a program, you may want to communicate between windows.

Say you have a main window and that main window creates a pop up window, for instance, when a button is clicked. This pop up window may ask the user for information such as a question. We then take the input from what the user has entered and bring it over to the main window for processing.

So this is one example where we need communication and transfer of data between windows in a JavaFX application.

In our code below, we have a main window and we have a pop up window created from the main window. This pop up window asks the user, "Do you want to go to the movies?"

There are 2 buttons, Yes and No.

If the user clicks on yes, we have a boolean variable that then is set equal to true.

If the user clicks on no, we have a boolean value that then is set equal to false.

We then transfer this boolean value from the pop up window to the main window and then can do anything based on the result. Specifically in our program, if the user clicks on the Yes button, then we create a label in the main window that will say, "Cool, let's see the hulk". If the user clicks on the No button, then we create a label in the main window that will say, "Okay, then, let's go to the park."

This provides great communication and data transfer among windows in JavaFX.

Java Pop Up Window File

So first, we'll write the code for the pop up 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.

The first thing we do is create a static boolean variable named answer. We use this variable later in our code to set this boolean value to either true if the user clicks yes or false if the user clicks no. We later transfer this boolean value to the main window.

In this class, we add a method named display() that returns a boolean value.

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 "Do you want to go to the movies?".

We then add the 2 buttons, the yes and no buttons, to this pop up window named yesbutton, which has a text value of "Yes" and nobutton, which has a text value of "No".

If the user clicks on the Yes button, an event handler is triggered so that the boolean variable answer is set equal to true. We also add a close() function so that the window closes after the user clicks on a button.

If the user clicks on the No button, an event handler is triggered so that the boolean variable answer is set equal to false. In the same way, we close the window.

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 create a static boolean variable named result. We use this variable later in the code so that we can store the result from the pop up window, where the user is asked if he or she wants to go to the movies.

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

We set the title to "JavaFX GUI".

We then create a label called labelmain.

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 store the output from the pop up window, where the user is asked if he or she wants to go to the movie in the result variable. Remember that the display() function returns a boolean value. This boolean value, of either true or false, is stored in the result variable.

We then take the result variable and form an if else statement based on it. If the value returned is equal to true, then we output, "Cool, let's see the hulk." If the value returned is equal to false, then we output, "Okay, then, let's go to the park instead".

You can see now how we've transferred data from one window (the pop up window) to another window (the main window) and process the information (using an if statement).

This is the central core of transferring data between windows.

Of course, we could have forgone the if else statements by making the display() function return a string directly instead of return a boolean which then processed the boolean value and returns a string. But this was done to show how to transfer data between windows and process the data obtained from a window and do whatever we want with it.

The rest of the code is just to finish the building of the program.

We then create a layout from the VBox 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...