DialogFX: A New Approach to JavaFX Dialogs

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 

Comments:

Good, its very good initiative

Posted by rogerio on September 03, 2012 at 12:32 PM PDT #

Hi Rogerio,

Thanks! The list of planned mods is already well underway, but hopefully this gets things off to a good start. All the best to you!

Mark

Posted by Mark on September 03, 2012 at 02:16 PM PDT #

Interested in moving this code into JFXtras? So valuable enhancements like these become a one stop shop for users?

Posted by Tom on September 04, 2012 at 12:10 AM PDT #

Tom,

Absolutely. Gerrit has already sent me some Builder code to add; I need to test it with a couple of other small changes and update what's out there, but then I'd be happy to add it to the mix. How should I proceed?

Thanks,
Mark

Posted by Mark Heckler on September 04, 2012 at 07:22 AM PDT #

The best thing is to subscribe to the jfxtras developers mailing list

https://groups.google.com/forum/?fromgroups#!forum/jfxtras-dev

And say hello, and that you would like to contribute. Things should be picked up from there and you'll end up with committer rights.

You can already check out the project without committer rights at:

https://github.com/JFXtras/jfxtras-labs

Posted by Tom on September 04, 2012 at 07:28 AM PDT #

Hi Mark! Thank you for this project, it's really awesome. More than anything else, I like the idea of showDialog() returning an int, so it can be handled accordingly. Anton's Dialog is great too, I'm currently using it in a project, but I had to make some modifications in order to handle user response. Once again, THANKS!

Posted by Slavko on September 10, 2012 at 03:31 AM PDT #

Hi Slavko,

Thank you for the kind words! I'm working on several updates to the library already, things that I just didn't have time to work into the first version or things people have offered as suggestions. Stay tuned, and please don't hesitate to pass along any ideas you may have as well. Happy coding!

Mark

Posted by Mark Heckler on September 10, 2012 at 07:09 AM PDT #

Is there any provision envisaged for getting rid of the OS buttons in the title bar of the dialogues so as to make the dialogues look more professional, i.e. not just for business apps?

Posted by Pete on September 11, 2012 at 09:51 AM PDT #

Hi Pete,

Actually, there is a growing list of enhancements/mods that we're working...and it's on the list.

Thanks for the feedback!

All the best,
Mark

Posted by Mark Heckler on September 11, 2012 at 10:02 AM PDT #

Nice work! Maybe this can become part of JFXtras? I was also looking for something similar with a "light-box" effect using a Service background Task and came up with this: ugate.wordpress.com/2012/05/16/javafx-dialog-service/

Posted by ugate on September 14, 2012 at 09:33 AM PDT #

Thanks for the kind words. It's now a part of JFXtras-labs, actually, and the list of planned extensions/enhancements continues to grow! Good luck, and please feel free to pass along any additional thoughts/suggestions you may have.

All the best,
Mark

Posted by Mark Heckler on September 14, 2012 at 09:48 AM PDT #

Mark -
Looks like a simple solution that I need in a few JavaFX applications I've done. These are using Zonski's javafs-flow framework (which I love) but his Dialog support is somewhat vague.
While building a DialogFX library, I encountered only one--probably simple--problem: My NetBeans 7.2 and JavaFX embedded in Java JDK 1.7_07 finds the Stage class but it's missing the showAndWait method! Where can I find the latest class lib/jar that includes this function?

Thanks for your work.
Best regards.
Bob

Posted by guest on October 01, 2012 at 10:17 PM PDT #

Hi Bob,

The showAndWait() method was introduced in JavaFX 2.2, which is bundled with JDK 7u7. Any chance you're still pointing to an older jfxrt.jar? From what you're saying, it should be there.

I'm updating the DialogFX git repo to avoid any disruption, but DialogFX is also now part of the open-source JFXtras project, along with a number of other tools you might find useful. Check it out if you'd like, and please sign up for the mailing list there. Feedback and code contributions always welcome. :-D

All the best,
Mark

Posted by Mark Heckler on October 01, 2012 at 11:24 PM PDT #

Hi Bob,

Mea culpa - I forgot the link. It's www.jfxtras.org. :-)

All the best,
Mark

Posted by Mark Heckler on October 01, 2012 at 11:27 PM PDT #

Thanks Mark
The proper Stage.class version IS there, of course, and running javap confirmed that. I cleaned out all of the older java folders and javaFX folders under "Oracle" (remember when that was the only way to install JavaFX?).
Now all is fine. For NetBeans users, there is still a complication because the so-called NB Default JavaFX Platform library does not contain the jfxrt.jar that has all the classes! I add another library that finds this in jdk1.7.0_07\jre\lib.

I should have learned my lesson by now: keep latest framework versions, but throw away or hide the old ones immediately.

Regards
Bob

Posted by guest on October 02, 2012 at 09:11 AM PDT #

Hi Bob,

That gets us all from time to time. Glad you nailed it down and are up and running!

All the best,
Mark

Posted by Mark Heckler on October 02, 2012 at 09:51 PM PDT #

congratulations, this is a great initiative !!

I suggest to allow set title and message in the constructor at once

Posted by Rafael Nascimento on October 29, 2012 at 10:13 AM PDT #

Thanks, Rafael! There actually is a way to set both title & message at once, using the DialogFXBuilder (Builder pattern). I hope to put a new post out soon with more details, but I'm trying to wrap up a change bundle before I do it so I can tackle more things in one shot...or a series of posts, if necessary.

Please stay tuned! Exciting things are in the wings...

All the best,
Mark

Posted by Mark Heckler on October 29, 2012 at 11:17 AM PDT #

buen aporte !

Posted by guest on December 06, 2012 at 01:31 AM PST #

how to catch event button"OK" or "NO"? thank

Posted by guest on January 17, 2013 at 12:25 PM PST #

Hi,

The primary goal for DialogFX was simple functionality, so the interaction with buttons was limited to accepting a return value indicating the number of the button pressed...and you could even ignore that, of course. :-)

DialogFX has really been superseded by MonologFX, which expands the capabilities of JavaFX dialogs with configurable iconography, hotkeys, specific return values (vs. positional), and a few other nice-to-haves. Here is a link to the posts about MonologFX:
https://blogs.oracle.com/javajungle/entry/monologfx_floss_javafx_dialogs_for
https://blogs.oracle.com/javajungle/entry/polishing_the_monologfx_api

I hope this helps. Good luck!

All the best,
Mark

Posted by Mark Heckler on January 17, 2013 at 01:50 PM PST #

Hi Mark

Thank you for the code as it has helped me a lot, however i am having a problem with the popup crashing the second time it appears
so for example am trying to save an imcomplete form , so an Information dialog appears and that works, then if i click again on save or any button that triggers a new FX Dialog loaded, it hangs and this happens on showAndWait() , I looked at previous comments about versions, and I have updated FX and Java versions to the latest and still no hope !
If you can help that be great

Regards
Aveen

Posted by guest on March 21, 2013 at 04:07 AM PDT #

Hi Aveen,

Thanks for the nice compliments! DialogFX was superseded by MonologFX, another open source library I created based upon feedback I got on DialogFX. I'd really recommend you "upgrade" to it and give it a try, as MonologFX is much better code and incorporates some features I wish I had included in its predecessor.

To take a look, please check out these articles:
https://blogs.oracle.com/javajungle/entry/monologfx_floss_javafx_dialogs_for
and
https://blogs.oracle.com/javajungle/entry/polishing_the_monologfx_api

Links to the code are in there, too. :-)

Please let me know what you think. Hope this helps!

All the best,
Mark

Posted by Mark Heckler on March 21, 2013 at 03:06 PM PDT #

That's great, but how to add html to this dialog?

Posted by Wang ZI on November 10, 2013 at 05:47 AM PST #

Hi,

I don't understand what you're wanting to do. Can you provide additional details, perhaps a use case or two? I plan to begin a significant rewrite of the dialogs (actually, MonologFX, which replaced DialogFX) this weekend, and if there's something useful I can add to the mix, I'll happily do it.

Thanks!

Mark

Posted by Mark Heckler on November 12, 2013 at 07:32 AM PST #

Hi Mark

Yes, maybe add a email link or paypal donate to the about me menu, or a hyperlink to your web help content page.

Regards,
Hua Jie

Posted by Wang Zi on November 12, 2013 at 07:42 AM PST #

Ah, that makes sense. Thanks for the clarification!

That's definitely something I want and need to do. It's now on the list...not sure if it will be in there by next week, but it WILL happen, and soon. Thank you for passing along the suggestion & information!

All the best,
Mark

Posted by Mark Heckler on November 12, 2013 at 08:14 AM PST #

This works. Thank you. And do you know any framework for confirmation dialog box?
Thank you

Posted by Pranish on January 10, 2014 at 04:00 AM PST #

This works for me. And do you know any files like these to show confirmation dialog box?
Thank you

Posted by Pranish on January 10, 2014 at 04:02 AM PST #

Hi Pranish,

Thank you! I'm not sure if I understand what you're wanting exactly, but I'd suggest you check out the dialogs I created to replace DialogFX, called MonologFX. You can get them from my GitHub repo or as part of JFXtras Labs. They have many more features than I had in DialogFX, and I plan to add many more when time permits.

Thank you again for your kind words, and best to you!

Mark

Posted by Mark Heckler on January 10, 2014 at 09:59 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

The Java Jungle addresses topics from mobile to enterprise Java, tech news to techniques, and anything even remotely related. The goal is to help us all do our work better with Java, however we use it.

Your Java Jungle guide is Mark Heckler, an Oracle Java/Middleware/Core Engineer with development experience in numerous environments. Mark's current work pursuits and passions all revolve around Java and leave little time to blog or tweet - but somehow, he finds time to do both anyway.

Mark lives with his very understanding wife, three kids, and dog in the St. Louis, MO area.



Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today