Sunday, August 19, 2012

Creating a MessageBox / Message Dialog in Java Swing

To create a dialog in Swing, you will need to use the JOptionPane class.

If you want to trigger a message dialog, you will need to use the static method:
showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)

1.The Parent Component

Defines the component (usually a JFrame or a JPanel) that will be the parent of the dialog. The coordinates of the parent will be used for the placement of the dialog box (usually the dialog box will to the middle of the component). If this parameter is null the dialog will pop-up in the center of the screen.

Frame Parent example:
package javaexample;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class DialogExample
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Sample frame");
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JOptionPane.showMessageDialog(frame, "Sample dialog box");
    }
}
The dialog box will pop-up in the center of the frame
Null Parent example:
package javaexample;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class DialogExample
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Sample frame");
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JOptionPane.showMessageDialog(null, "Sample dialog box");
    }
}
If the parentComponent parameter is null, the dialog box will pop-up to the center of the screen.
2.The Message

The message is  usually a string constant and represents the text contained in the body.

If message is an array, it will be interpreted as a series of messages (the interpretation is recursive - each object will be interpreted according to its type).  If message is a component, the component will be displayed in the dialog.

Message example:
package javaexample;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class DialogExample
{
    public static void main(String[] args)
    {
        Object[] objects = {UIManager.getIcon("OptionPane.questionIcon"),
                            "This", 
                            "is", 
                            "the", 
                            "new", 
                            new JButton("Hit")};
        JOptionPane.showMessageDialog(null, objects, 
                                     "This is the title",
                                      JOptionPane.PLAIN_MESSAGE);
    }
}
Message Example
3.The Message Type

The messageType defines the style of message. The available options are:
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE

Message example:
package javaexample;

import javax.swing.JOptionPane;

public class DialogExample
{
    public static void main(String[] args)
    {
        new Thread()
        {
            @Override
            public void run()
            {
                JOptionPane.showMessageDialog(null, "Error message", 
                                              "This is the title",
                                              JOptionPane.ERROR_MESSAGE);
            }
        }.start();
        new Thread()
        {
            @Override
            public void run()
            {
                JOptionPane.showMessageDialog(null, "Information message", 
                                              "This is the title",
                                              JOptionPane.INFORMATION_MESSAGE);
            }
        }.start();
        new Thread()
        {
            @Override
            public void run()
            {
                JOptionPane.showMessageDialog(null, "Warning message", 
                                              "This is the title",
                                              JOptionPane.WARNING_MESSAGE);
            }
        }.start();
        new Thread()
        {
            @Override
            public void run()
            {
                JOptionPane.showMessageDialog(null, "Question message", 
                                              "This is the title",
                                              JOptionPane.QUESTION_MESSAGE);
            }
        }.start();
        new Thread()
        {
            @Override
            public void run()
            {
                JOptionPane.showMessageDialog(null, "Plain message", 
                                              "This is the title",
                                              JOptionPane.PLAIN_MESSAGE);
            }
        }.start();
    }
}
The program above will generate 5 message dialogs
4.The Icon

You can place a decorative icon in the dialog box. Usually a default value is determined by the messageType parameter.
package javaexample;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class DialogExample
{
    public static void main(String[] args)
    {
        JOptionPane.showMessageDialog(null, "This is the text body", 
                                     "This is the title",
                                      JOptionPane.PLAIN_MESSAGE,
                                      UIManager.getIcon("Tree.collapsedIcon"));
    }
}
Icon example

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger