ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to implement JOptionPane Message Dialogs in Java swing framework ?.

Updated on September 17, 2014

In this Java programming tutorial, you will learn concept related to "Message Dialog" box in Java Swing API. "JOptionPane" class present in Java Swing API has all built in features to implement "Message Dialog" boxes. "JOptionPane" class is being used to create output and input dialog boxes. "Message Dialog" is one of the type of box supported by JOptionPane class.

Message Dialog - It generally displays a message and waits till user clicks "OK".

In order to demonstrate various features of JOptionPane and Message Dialog box, below is a video tutorial. This video shows how to code different types of Message Dialog boxes.

Video Tutorial - Message Dialog Box

Java Source Code Demo - Message Dialog Box

Let's look into Java source code for Message Dialog box implementation. Below is the complete Java source code for implementing various Message Dialog boxes present in Swing API.

Message Dialog Box Source Code -

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class MessageDialog {

   public static void main(String[] args) {
	// It displays a message "Hello Hubpages !!!"
	// It waits till user clicks on "OK".
	JOptionPane.showMessageDialog(null, "Hello Hubpages !!!");

	// It displays a message "Hello Hubpages !!!" with a title as
	// "Welcome". It does not show any icon, just plain message.
	// It waits till user clicks on "OK".
	JOptionPane.showMessageDialog(null, "Hello Hubpages !!!", "Welcome",JOptionPane.PLAIN_MESSAGE);

	// It displays an information message "Hello Hubpages !!!" 
	// with a title as "Welcome". 
	// It shows an icon indicating information.
	// It waits till user clicks on "OK".
	JOptionPane.showMessageDialog(null, "Hello Hubpages !!!", "Welcome",JOptionPane.INFORMATION_MESSAGE);

	// It displays a warning message "Hello Hubpages !!!" with a title as
	// "Welcome". It shows an icon indicating warning.
	// It waits till user clicks on "OK".
	JOptionPane.showMessageDialog(null, "Hello Hubpages !!!", "Welcome",JOptionPane.WARNING_MESSAGE);

	// It displays a question message "Hello Hubpages !!!" with a
	// title as "Welcome". It shows an icon indicating a question.
	// It waits till user clicks on "OK".
	JOptionPane.showMessageDialog(null, "Hello Hubpages !!!", "Welcome",JOptionPane.QUESTION_MESSAGE);

	// It displays an error message "Hello Hubpages !!!" with a
	// title as "Welcome". It shows an icon indicating an error.
	// It waits till user clicks on "OK".
	JOptionPane.showMessageDialog(null, "Hello Hubpages !!!", "Welcome",JOptionPane.ERROR_MESSAGE);
	
        ImageIcon icon = new ImageIcon("CD.png");

	// It displays a message "Hello Hubpages !!!" with a
	// title as "Welcome". It shows an customized icon and 
	// information related to it.
	// It waits till user clicks on "OK".
	JOptionPane.showMessageDialog(null, "Hello Hubpages !!!", "Welcome",JOptionPane.INFORMATION_MESSAGE, icon); 

	}
}

Output of the program -

As soon as, you run the program, it displays different types of message dialog boxes in order. Let's discuss output and different types of message dialog box.

Types of Message Dialog Boxes

Message Dialog box are of various types. The different types of Message Dialog are as follows :

Information message box with default title

It displays a message "Hello Hubpages !!!". It shows a default title as "Message". It waits till user clicks on "OK". The image is shown below :

Plain Message Dialog Box

It displays a message "Hello Hubpages !!!" with a title as "Welcome". It does not show any icon, just plain message. It waits till user clicks on "OK". The image is shown below :

Information Message Box with a title

It displays an information message "Hello Hubpages !!!" with a title as "Welcome". It shows an icon indicating information. It waits till user clicks on "OK". The image is shown below :

Warning Message Dialog Box

It displays a warning message "Hello Hubpages !!!" with a title as "Welcome". It shows an icon indicating warning. It waits till user clicks on "OK". The image is shown below :

Question Message Dialog Box

It displays a question message "Hello Hubpages !!!" with a title as "Welcome". It shows an icon indicating a question. It waits till user clicks on "OK". The image is shown below :

Error Message Dialog Box

It displays an error message "Hello Hubpages !!!" with a title as "Welcome". It shows an icon indicating an error. It waits till user clicks on "OK". The image is shown below :

Customized Information Dialog Box

It displays a message "Hello Hubpages !!!" with a title as "Welcome". It shows an customized icon and information related to it. It waits till user clicks on "OK". This message dialog box gives user a way to show its custom images on the dialog box. The image is shown below :

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article