When programming a graphical user interface (GUI) there are occasions where you’ll need a simple popup dialog to communicate with the user. In Swing (the predecessor of JavaFX) there is a convenient class called JOptionPane for such dialogs. A similar class doesn’t exist in JavaFX 2.x (yet).
Fortunately, the authors of JavaFX published some user interface controls they are currently working on. Those controls might be added in a future version of JavaFX.
One of those ui controls is a class called Dialogs.java which is exactly what we need.
How To Use the Dialogs
Download the newest javafx-dialogs-x.x.x.jar file from my GitHub Page. I put all necessary classes, css files and images inside this jar.
Add the jar file to your project (usually inside a lib subfolder).
Add the jar file to the project’s classpath: In Eclipse right-click on the jar file | Build Path | Add to Build Path. Now Eclipse knows about the library.
Then add one of the following lines:
Information Dialog
12
Dialogs.showInformationDialog(stage,"I have a great message for you!","Information Dialog","title");
Warning Dialog
1
Dialogs.showWarningDialog(stage,"Careful with the next step!","Warning Dialog","title");
Error Dialog
1
Dialogs.showErrorDialog(stage,"Ooops, there was an error!","Error Dialog","title");
You may also provide an exception:
12
Dialogs.showErrorDialog(stage,"Ooops, there was an error!","Error Dialog With Exception","title",newFileNotFoundException("Could not find file blabla.txt"));
Confirm Dialog
12
DialogResponseresponse=Dialogs.showConfirmDialog(stage,"Do you want to continue?","Confirm Dialog","title");
You may provide DialogOptions like this:
12
DialogResponseresponse=Dialogs.showConfirmDialog(stage,"Are you ok with this?","Confirm Dialog With Options","title",DialogOptions.OK_CANCEL);
The response will either be DialogResponse.YES, DialogResponse.NO, DialogResponse.CANCEL, DialogResponse.OK, or DialogResponse.CLOSED.
Input Dialog
1
Stringinput=Dialogs.showInputDialog(stage,"Please enter your name:","Input Dialog","title");
If you provide some choices, a combobox will be displayed:
1234567
List<String>choices=newArrayList<>();choices.add("a");choices.add("b");choices.add("c");Stringinput=Dialogs.showInputDialog(stage,"Choose your color:","Input Dialog With Choices","title","b",choices);
UPDATE (Version 0.0.3)
Custom Dialog
Since JavaFX dialogs version 0.0.3 there is support for custom dialogs (thanks to Guldner for providing the patch).
Here is an example of how to use custom dialogs to create a login form:
GridPanegrid=newGridPane();grid.setHgap(10);grid.setVgap(10);grid.setPadding(newInsets(0,10,0,10));finalTextFieldusername=newTextField();username.setPromptText("Username");finalPasswordFieldpassword=newPasswordField();password.setPromptText("Password");grid.add(newLabel("Username:"),0,0);grid.add(username,1,0);grid.add(newLabel("Password:"),0,1);grid.add(password,1,1);StringusernameResult;StringpasswordResult;Callback<Void,Void>myCallback=newCallback<Void,Void>(){@OverridepublicVoidcall(Voidparam){usernameResult=username.getText();passwordResult=password.getText();returnnull;}};DialogResponseresp=Dialogs.showCustomDialog(stage,grid,"Please log in","Login",DialogOptions.OK_CANCEL,myCallback);System.out.println("Custom Dialog: User clicked: "+resp);//You must check the resp, since input fields' texts are returned regardless of what button was pressed. (ie. If user clicked 'Cancel' disregard the input) System.out.println("Custom Dialog: Fields set from custom dialog: "+usernameResult+"/"+passwordResult);
Bugs / Questions
If you have questions or found a bug please leave a comment below or report an issue on GitHub.