This tutorial will lead you through the basics of creating message dialogs, input dialogs, and option dialogs in Java. You will see the power of the JOptionPane object from the javax.swing package. With this, almost all possible dialog boxes can be easily created. This is a powerful tool to make the program interact with the user.
The message dialog
A message dialog is simply a dialog that shows some information to the user. Simply a box with text, and you click OK to close it. Here is a perfect example of this:
import javax.swing.*; JOptionPane.showMessageDialog (null, "This is a message box");
Let's have a look at the parameters:
1) Specifies the window in which the dialog is shown. Null means it's shown at the middle of the screen.
2) Specifies the text to be shown in the box.
Of course, more elaborate dialogs are possible. Let's have a look at the standard version:
import javax.swing.*; int mc = JOptionPane.WARNING_MESSAGE; JOptionPane.showMessageDialog (null, "Message here.", "Title here", mc);
The function has now 4 parameters. This is not much complicated, so let's explain them:
1) Specifies the window. Null means the center of the screen.
2) The text shown in the box.
3) The title of the box
4) The icon type (see below).
The icon types are:
WARNING_MESSAGE: A yellow triangle with an exclamation mark.
QUESTION_MESSAGE: A question mark.
ERROR_MESSAGE: A stop sign with a X in it.
INFORMATION_MESSAGE: A purple circle with an I in it.
PLAIN_MESSAGE: No icon.
The input dialog
Well, the first one was quite easy. Now let's add some difficulty with the input dialog. This allows the user to type text in a text field inside the dialog box. The code:
import javax.swing.*; String str = JOptionPane.showInputDialog (null, "Input text:");
The parameters mean the same thing than the first example. Now we will see the more elaborate form:
import javax.swing.*; int mc = JOptionPane.WARNING_MESSAGE; String str = JOptionPane.showInputDialog (null, "Enter text:", "Title", mc);
Pretty easy, like the first form again. The parameters do not change:
1) Specifies the window. Null means the center of the screen.
2) The text shown in the box.
3) The title of the box
4) The icon type (see above).
Note: the function will return the text entered, or null if cancel was clicked or the box was closed. If you do not enter anything and click OK, an empty string is returned.
The confirm dialog
Sometimes, to perform important operations such as deleting files or closing the program, a confirmation box could be mandatory. You can choose between "OK-Cancel", "Yes-No", and many other dialogs. Again, let's have a look at a basic form:
import javax.swing.*; int choice = JOptionPane.showConfirmDialog (null, "Choose option below");
Do you guess what? Yes! The two parameters are, again, the same as the first example! However, multiple buttons are shown at the bottom of the box, making it possible for the user to select something to do. Hopefully, a form allowing you to select which buttons to show, the icon, and title is avaliable, like this one:
import javax.swing.*; int mc = JOptionPane.QUESTION_MESSAGE; int bc = JOptionPane.YES_NO_CANCEL_OPTION; int ch = JOptionPane.showConfirmDialog (null, "Select:", "Title", bc, mc);
Oh, 5 parameters this time! This does not make the things more complicated:
1) Specifies the window. Null means the center of the screen.
2) The text shown in the box.
3) The title of the box
4) The buttons (see below).
5) The icon (see above, it stays the same).
Something new is here: the buttons. Well, use one of these constants to define them:
JOptionPane.YES_NO_OPTION: Yes, no.
JOptionPane.YES_NO_CANCEL_OPTION: Yes, no, cancel.
JOptionPane.OK_CANCEL_OPTION: Ok, cancel.
Note: the function will return the button pressed.
JOptionPane.OK_OPTION: OK was clicked.
JOptionPane.CANCEL_OPTION: Cancel was clicked.
JOptionPane.YES_OPTION: Yes was clicked.
JOptionPane.NO_OPTION: No was clicked.
JOptionPane.CLOSED_OPTION: The box was closed.
The option dialog
The last but not the least is the option dialog. Many buttons are shown, and you can select one. We will see the standard form, the one you will often use:
import javax.swing.*; int mc = JOptionPane.QUESTION_MESSAGE; String[] opts = {"Red", "Blue", "Green", "Yellow", "Black", "White"}; int ch = JOptionPane.showOptionDialog (null, "Choose", "Title", 0, mc, null, opts, opts[2]);
Now a whooping 8 parameters. What are they? That's what we'll see!
1) Specifies the window. Null means the center of the screen.
2) The text shown in the box.
3) The title of the box.
4) Unused for OptionDialog, use 0.
5) The icon (see above, again the same).
6) The icon object, use null for this one.
7) The buttons shown, a string array must be supplied.
8) The default button selected.
Now, you can see an array. Why? Well, since the Java language does not know how many buttons you will put, an array must be supplied. The opts array we used contains the 6 buttons, which are color names. The last parameter will tell the default selected button. If, for example, we want the third button selected, we would use opts[2], because arrays start their offset at zero.
The function will return the offset of the button. For example, if you click the third button, it returns 2. If you click the first, it returns 0. Again, this is due to the array's offset which starts at 0. To print the text of the returned button, you can use this:
System.out.println ("You clicked " + opts[ch]);
Where ch is the value returned by the dialog. For more security, I recommend using:
if (ch >= 0){ System.out.println ("You clicked " + opts[ch]); } else{ System.out.println ("You closed the box."); }
If you close the box, it returns -1, thus not matching the condition (ch >= 0).
Conclusion
That's it, we arrived to the end of the tutorial. Even if this may seem complicated at first, I feel we have barely scratched the surface. Java is a powerful language, the possibilites are unlimited. Custom boxes can be created, colors, and so on. If you misunderstand something, do not hesitate to contact me.