How To Create Alert Dialog Like JOptionPane In Java FX 2

As of this writing, the answer is no. It is weird that the Java FX team did not include classes that developers can easily call to display message alerts like those in the JOptionPane class.

The workaround? Create your own. Well, the Java FX team did release a JAR file of a few classes, alert dialogs included but these look horrible. So the only solution was for me to create my own.

This is what I came up with.

alertdialog_javafx

 

I know, it looks simple. But you can easily make it look better using CSS. I only provided the basic for you to get started.

Here are the codes.

And this is the css file.

To use it, just call it like this:

If you check the class, there is a method called getImage(). You will have to find your own error and info icons though ;).

The alert dialog will have a height of 100 pixels. If you put a long text, there is no need to worry because the alert dialog will expand in width while maintaining a max of 2 lines ;).

Lastly, the last few lines of the constructor are needed so that the alert dialog will always stay at the center on top of its parent Stage.

Related Posts Plugin for WordPress, Blogger...

Post a Comment