Page 1 of 1

message dialogs in java MessageDialog, OptionDialog, ConfirmDialog, InputDialog Rate Topic: -----

#1 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Posted 08 January 2007 - 12:28 PM

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:

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.

Is This A Good Question/Topic? 1
  • +

Replies To: message dialogs in java

#2 Shinilolz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 238
  • Joined: 30-November 06

Posted 09 January 2007 - 07:51 PM

Nice, Tutorial :)
Was This Post Helpful? 1
  • +
  • -

#3 ZacharyDavid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-September 08

Posted 13 September 2008 - 04:41 PM

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.
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 18 September 2008 - 03:22 PM

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 September 2008 - 03:23 PM

Was This Post Helpful? 1
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2636
  • View blog
  • Posts: 15,656
  • Joined: 24-September 08

Posted 26 September 2008 - 10:16 AM

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.
Was This Post Helpful? 0
  • +
  • -

#6 happycamper  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 11-November 08

Posted 24 February 2009 - 06:06 PM

Cool tutorial, thanks!
Was This Post Helpful? 0
  • +
  • -

#7 conkiller1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-May 09

Posted 19 May 2009 - 05: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
Was This Post Helpful? 0
  • +
  • -

#8 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Posted 19 May 2009 - 06:02 AM

View Postconkiller1, on 19 May, 2009 - 07:59 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#9 conkiller1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-May 09

Posted 19 May 2009 - 06:07 AM

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 :D

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 - 06:30 AM

Was This Post Helpful? 0
  • +
  • -

#10 sponge6546  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-May 09

Posted 29 May 2009 - 11:05 AM

Hey alpha,
Thank you for posting this tutorial. It really helped me out. :D
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Corey Jones*


Reputation:

Posted 08 March 2010 - 02:48 PM

View Postconkiller1, on 19 May 2009 - 05:07 AM, said:

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 :D

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?


Did you ever find a way to /n the options? I've been searching everywhere for a way to do this and no luck so far.
Was This Post Helpful? 0

Page 1 of 1