Thursday, October 30, 2014

JavaFX dialogs

The JavaFX project doesn't have dialog controls, so when I wanted to add dialogs to my project I used the dialog controls from the fxexperience project, they release you from all the trouble of creating your own control, but recently they announced that the dialog controls will be in a separate project (announcing-controlsfx-8-20-7) deprecating the current controls.

The new project is called openjfx dialogs and it is located right here https://bitbucket.org/controlsfx/openjfx-dialogs. Since right now it doesn't contain any documentation I will explain you how to start using it and ease the way to start adopting them.

To start working first download the project repository from here bitbucket.org/controlsfx/openjfx-dialogs, in the left side there is a link to download, it downloads a zip file, unzip it wherever you wan to.

Next the project needs to be build, to do this we will need gradle, for thoose who don't know what is gradle here it is a brief description about it:

"Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build"

I'm not going to explain deeper gradle since it is another topic and this post wouldn't be enough to explain it.

You can download and install gradle form here www.gradle.org, the project already contains a version of gradle you can just run the gradlew.bat file. Either you install it or use the version of the project, run the following command at the root of the project:

    gradle build

If the project is built correctly you will find the folders build/lib under the root of the project, under these folders the openjfx-dialogs-X.X.X.jar is located we will need this jar to add the dialog controls.

Now let's create an example to test the dialogs an see how they work, I'm going to use Netbeans to develop this example.

Let's create a javafx application project:


Let's name the project Dialogs, keep selected the checkbox "Create application class", then click finish.

Add to project the openjfx-dialogs jar, to do this over the project right click and select "properties" option. In the properties select the "libraries" option


Click over the Add Jar/Folder button, and select the jar previously built "openjfx-dialogs-X.X.X.jar".


With these steps everything is set to start creating dialogs, in the previous version the code to create a dialog was kind of messy and not intuitive from of my point of view, this is an example:

It uses the builder pattern wikipedia.org/wiki/Builder_pattern, a single class did handle all the types of dialogs, but now this code is deprecated. You can find more about the deprecated dialogs right here controlsfx.bitbucket.org/index.html?org/controlsfx/dialog/Dialogs.html

In the new version each type of dialog has its own class making the code clearer and more intuitive, this is from my point of view. At this point the types of dialogs are javafx.scene.control.Alert, javafx.scene.control.ChoiceDialog and javafx.scene.control.TextInputDialog.

Lets create an alert dialog, in the class created in the project change it to display the dialog as the following example:

From the example above an Alert object is created and it is passed a type of alert dialog, in this case is confirmation but there are others as error, information, warning, and so on.

In the button's click event the show() is called to display the dialog, but this method doesn't wait the dialog to close, the method showAndWait() waits the dialog to close and returns an Optional object with the button typed if that was the case.

The following example uses a text input dialog:

From the example above the only difference is that the showAndWait() method returns an Optional object with the content of the input element from the dialog.

The ChoiceDialog displays a dialog with a combo control with a set of values, the following example show how to use it:

From the example above you can see that the options are passed through the constructor and later through the items property and uses the same mechanism as the TextInputDialog to retrieve the value selected.

As I said before since the project is not finish and right now there is no documentation, I hope that with this post you can more easily adopt this new version or start using it.

1 comment:

  1. How does one test dialog.showAndWait(). The application will block on showAndWait() and the test code won't execute.

    ReplyDelete