X

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 

Join the discussion

Comments ( 32 )
  • rogerio Monday, September 3, 2012

    Good, its very good initiative


  • Mark Monday, September 3, 2012

    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


  • Tom Tuesday, September 4, 2012

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


  • Mark Heckler Tuesday, September 4, 2012

    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


  • Tom Tuesday, September 4, 2012

    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


  • Slavko Monday, September 10, 2012

    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!


  • Mark Heckler Monday, September 10, 2012

    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


  • Pete Tuesday, September 11, 2012

    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?


  • Mark Heckler Tuesday, September 11, 2012

    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


  • ugate Friday, September 14, 2012

    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/


  • Mark Heckler Friday, September 14, 2012

    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


  • guest Tuesday, October 2, 2012

    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


  • Mark Heckler Tuesday, October 2, 2012

    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


  • Mark Heckler Tuesday, October 2, 2012

    Hi Bob,

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

    All the best,

    Mark


  • guest Tuesday, October 2, 2012

    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


  • Mark Heckler Wednesday, October 3, 2012

    Hi Bob,

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

    All the best,

    Mark


  • Rafael Nascimento Monday, October 29, 2012

    congratulations, this is a great initiative !!

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


  • Mark Heckler Monday, October 29, 2012

    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


  • guest Thursday, December 6, 2012

    buen aporte !


  • guest Thursday, January 17, 2013

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


  • Mark Heckler Thursday, January 17, 2013

    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


  • guest Thursday, March 21, 2013

    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


  • Mark Heckler Thursday, March 21, 2013

    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


  • Wang ZI Sunday, November 10, 2013

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


  • Mark Heckler Tuesday, November 12, 2013

    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


  • Wang Zi Tuesday, November 12, 2013

    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


  • Mark Heckler Tuesday, November 12, 2013

    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


  • Pranish Friday, January 10, 2014

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

    Thank you


  • Pranish Friday, January 10, 2014

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

    Thank you


  • Mark Heckler Friday, January 10, 2014

    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


  • Mile1 Sunday, June 22, 2014

    Hi, just wanted to say great job and thank you. This dialog works great. I have just two questions.

    1. How do you set action listener on yes or no button?

    2. How can I use icons from some other directory, I trieg putting in the name of iconFile object absolute and relative paths, and nothing happens?

    Thank you very much


  • Mark Heckler Monday, June 23, 2014

    Hi Mile,

    Thank you for the kind words! DialogFX was something I whipped up over a weekend to provide limited (but hopefully very useful) functionality for general dialog requirements. I didn't "bake into" the library a lot of nice-to-have functionality, such as replaceable icons or the ability to set action listeners as part of the dialog operations.

    I did enhance & replace DialogFX with MonologFX, a more-capable library I wrote about on this blog as well at:

    * https://blogs.oracle.com/javajungle/entry/monologfx_floss_javafx_dialogs_for

    * https://blogs.oracle.com/javajungle/entry/polishing_the_monologfx_api

    * https://blogs.oracle.com/javajungle/entry/monologfx_update_timed_javafx_dialogs

    Truth be told, with dialogs being incorporated into subsequent JavaFX libraries/releases, I didn't expect much continued use of these libraries. The source code is on GitHub, and you're welcome to update & issue a pull request, fork the project, etc...and it's possible I'll come back and rework it to make it into an "ideal" (well, to my mind, anyway!) dialog package. Just not sure if there is sufficient reason to do so.

    I'd appreciate your (and others') thoughts on the matter. Please let me know! And thank you again for the kind words, glad you're finding it useful.

    Cheers,

    Mark


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services