Java Message Box

In this section we will use JOptionPane class to display the message Dialog box.

In this section we will use JOptionPane class to display the message Dialog box.

Java Message Box

In this tutorials you will learn about how to create Message Box in Java.
Java API provide a class to create a message Box in java. By message box we mean
a box in which message is displayed or a box from which input provided by the
user can be retrieved. A message box is meant to carry a temporary information
or message apart from the main swing application. Most of the message box is
used for error message. Using JOptionPane class you can create
a message box. Here is the code that creates Message box and shows it:

JOptionPane.showMessageDialog(frame, "This is a Message Box.");

This statement will display a message with message as "This is a Message Box.".

There are several feature JOptionPane like customizing icon,
customizing the component the dialog display and specify where message box
should appear on screen.

JOptionPane icon let you decide which icon you want to display. You can use
custom icon, or no icon, or any of the four JOptionPane standard icon. The two
most useful method of showxxxdialog method are as follows :

showMessageDiaolog : The showMessageDialog method
will display the one button dialog method.

showOptionDialog : This method display a
variety of button with button text. and contain a standard text message.

showConfirmDialog : It will ask for confirmation of
something with Yes/No.

showInputDialog : This will display a message box that
accept input from user from text field or combo box.

showMessageDialog : Display a message with one button which
is labeled "OK". You can specify the message, title, and icon according to your
wish. Here are some example using showMessageDialog :

JOptionPane () : Creates a JOptionPane with the
specified buttons, icons, message, title, and so on.

Example : We create a program which will display the message
box using a swing. First creating a swing application in which we created a
button. When you click on the button then message box will pop up and display a
message.