JOptionPane vs. JDialog

Kristjan Toots picture Kristjan Toots · Feb 17, 2012 · Viewed 9.9k times · Source

This is a crosspost to the thread in Javaranch (includes some images): http://www.coderanch.com/t/567472/GUI/java/Optimal-solution-creating-multiple-dialog

I'm trying to develop a simple swing desktop application where I imagine alot of different dialog's jumping around to fetch user input. Would need to present labels, textfields, passwordfields, combobxes, checkboxes etc in various dialog windows.

For example: creating the database firsthand, creating the first admin account, adding users, changing user accounts etc.

I have an understanding that JOptionPane is used to create simple quick & easy modal dialog's. I would really like to know why one would choose one over another in this case. Which one is more preferable to use: JOptionPane vs. JDialog

Also I could use some pointers how one should appropriately design and implement this.

Thank you.

Answer

Logan picture Logan · Feb 17, 2012

Here's a statement I found on the Java website that says one key point about the difference between the two.

How to make Dialogs

A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.

So it sounds like you would use JOptionPane if you want a user to have to make a choice and close the box before returning to the main screen. If you use a JDialog box, then they can just click around it and get back to the main screen without making a choice. For example, say you wanted to make a user choose the number of results before clicking submit, you wouldn't want them to be able to click around that window and click submit. You would use JOptionPane to force them to select a value first before going back to submit.