DialogFX: A New Approach to JavaFX Dialogs

Posted by HecklerMark on Oracle Blogs See other posts from Oracle Blogs or by HecklerMark
Published on Mon, 3 Sep 2012 17:01:39 +0000 Indexed on 2012/09/03 21:44 UTC
Read the original article Hit count: 586

Filed under:

How would you like a quick and easy drop-in dialog box capability for JavaFX? That's what I was thinking when a weekend presented itself. And never being one to waste a good weekend...  :-)

After doing some "roll-your-own" basic dialog building for a JavaFX app, I recently stumbled across Anton Smirnov's work on GitHub. It was a good start, but it wasn't exactly what I was after, and ideas just kept popping up of things I'd do differently. I wanted something a bit more streamlined, a bit easier to just "drop in and use". And so DialogFX was born.

DialogFX wasn't intended to be overly fancy, overly clever - just useful and robust. Here were my goals:

  • Easy to use. A dialog "system" should be so simple to use a new developer can drop it in quickly with nearly no learning curve. A seasoned developer shouldn't even have to think, just tap in a few lines and go. Why should dialogs slow "actual development"?  :-)
  • Defaults. If you don't specify something (dialog type, buttons, etc.), a good dialog system should still work. It may not be pretty, but it shouldn't throw gears.
  • Sharable. It's all open source. Even the icons are in the commons, so they can be reused at will.

Let's take a look at some screen captures and the code used to produce them.

 

DialogFX INFO dialog

Screen captures

Windows


Mac 

Sample code

        DialogFX dialog = new DialogFX();
        dialog.setTitleText("Info Dialog Box Example");
        dialog.setMessage("This is an example of an INFO dialog box, created using DialogFX.");
        dialog.showDialog();

DialogFX ERROR dialog

Screen captures

Windows


Mac 

Sample code

        DialogFX dialog = new DialogFX(Type.ERROR);
        dialog.setTitleText("Error Dialog Box Example");
        dialog.setMessage("This is an example of an ERROR dialog box, created using DialogFX.");
        dialog.showDialog();

DialogFX ACCEPT dialog

Screen captures

Windows


Mac 

Sample code

        DialogFX dialog = new DialogFX(Type.ACCEPT);
        dialog.setTitleText("Accept Dialog Box Example");
        dialog.setMessage("This is an example of an ACCEPT dialog box, created using DialogFX.");
        dialog.showDialog();

DialogFX Question dialog (Yes/No)

Screen captures

Windows


Mac 

Sample code

        DialogFX dialog = new DialogFX(Type.QUESTION);
        dialog.setTitleText("Question Dialog Box Example");
        dialog.setMessage("This is an example of an QUESTION dialog box, created using DialogFX. Would you like to continue?");
        dialog.showDialog();

DialogFX Question dialog (custom buttons)

Screen captures

Windows


Mac 

Sample code

        List<String> buttonLabels = new ArrayList<>(2);
        buttonLabels.add("Affirmative");
        buttonLabels.add("Negative");

        DialogFX dialog = new DialogFX(Type.QUESTION);
        dialog.setTitleText("Question Dialog Box Example");
        dialog.setMessage("This is an example of an QUESTION dialog box, created using DialogFX. This also demonstrates the automatic wrapping of text in DialogFX. Would you like to continue?");
        dialog.addButtons(buttonLabels, 0, 1);
        dialog.showDialog();

A couple of things to note

You may have noticed in that last example the addButtons(buttonLabels, 0, 1) call. You can pass custom button labels in and designate the index of the default button (responding to the ENTER key) and the cancel button (for ESCAPE). Optional parameters, of course, but nice when you may want them.

Also, the showDialog() method actually returns the index of the button pressed. Rather than create EventHandlers in the dialog that really have little to do with the dialog itself, you can respond to the user's choice within the calling object. Or not. Again, it's your choice.  :-)

And finally, I've Javadoc'ed the code in the main places. Hopefully, this will make it easy to get up and running quickly and with a minimum of fuss.

How Do I Get (Git?) It?

To try out DialogFX, just point your browser here to the DialogFX GitHub repository and download away! Please take a look, try it out, and let me know what you think. All feedback welcome!

All the best,

Mark 

© Oracle Blogs or respective owner

Related posts about /JavaDev