Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,433 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,505 people online right now. Registration is fast and FREE... Join Now!




message dialogs in java

 
Reply to this topicStart new topic

> message dialogs in java, MessageDialog, OptionDialog, ConfirmDialog, InputDialog

alpha02
Group Icon



post 8 Jan, 2007 - 11:28 AM
Post #1


Introduction

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:

CODE

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:

CODE

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:

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:

CODE

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:

CODE

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:

CODE

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:

CODE

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:

CODE

System.out.println ("You clicked " + opts[ch]);


Where ch is the value returned by the dialog. For more security, I recommend using:

CODE

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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

UnholyStar
Group Icon



post 9 Jan, 2007 - 06:51 PM
Post #2
Nice, Tutorial smile.gif
Go to the top of the page
+Quote Post

ZacharyDavid
*



post 13 Sep, 2008 - 03:41 PM
Post #3
I don't understand how to do this. When I typed:

import javax.swing.*;

it gave me an error. I was wondering what import means, and is javax.swing.* the file name? I want to learn Java on my own because I thought it would be a good thing to pick up. I wanted to try to make a message dialog, and I'm failing. What does the * mean in this code?

I was wondering what this code does:

JOptionPane.showMessageDialog (null, "This is a message box");

what is the JOptionPane.showMessage Dialog? I typed that as well, and it gave an error.
Go to the top of the page
+Quote Post

Locke
Group Icon



post 18 Sep, 2008 - 02:22 PM
Post #4
When you import...javax.swing.*;, that means you are importing all things in the javax.swing package. Nothing from higher or subsequent packages.

The * is a wild card. So Java imports all of the classes in that package, not just specific ones.

I don't know why those are giving you errors...they are right.

This post has been edited by Locke37: 18 Sep, 2008 - 02:23 PM
Go to the top of the page
+Quote Post

NeoTifa
Group Icon



post 26 Sep, 2008 - 09:16 AM
Post #5
i may still be in my forth week of my java class, but i always just used

import javax.swing.JOptionPane;

for JOP. the only time i use * is for the file class.
Go to the top of the page
+Quote Post

happycamper
**



post 24 Feb, 2009 - 05:06 PM
Post #6
Cool tutorial, thanks!
Go to the top of the page
+Quote Post

conkiller1
*



post 19 May, 2009 - 04:59 AM
Post #7
I"m trying to use the inputdialog but i need to input an int instead of a String, is there an easy way to do that?

good tutorial btw it helped me finish the crytpography project i'm working on the last part is the thing with the ints
Go to the top of the page
+Quote Post

William_Wilson
Group Icon



post 19 May, 2009 - 05:02 AM
Post #8
QUOTE(conkiller1 @ 19 May, 2009 - 07:59 AM) *

I"m trying to use the inputdialog but i need to input an int instead of a String, is there an easy way to do that?

good tutorial btw it helped me finish the crytpography project i'm working on the last part is the thing with the ints

In short, no. You have to take a String input, then verify that it is an integer before moving on in your application.
Go to the top of the page
+Quote Post

conkiller1
*



post 19 May, 2009 - 05:07 AM
Post #9
hrmm... what if i did the option dialog with 28 different options, giving them titles from 0-26 and cancel, and then used the given index. would that work? ( working on it now... )

YATTA! it worked biggrin.gif

Sorry to keep bugging you guys with all these edits.

I have now reached an interesting problem more aesthetic than anything, my JOptionPane goes out both sides of my monitor and in order to reach the beginning or ending numbers you have to move it back and forth, anyway to \n the options?

This post has been edited by conkiller1: 19 May, 2009 - 05:30 AM
Go to the top of the page
+Quote Post

sponge6546
*



post 29 May, 2009 - 10:05 AM
Post #10
Hey alpha,
Thank you for posting this tutorial. It really helped me out. biggrin.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 12:49AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month