3 Ways to Show JOptionPane Message Dialog

Screenshot of 2nd message dialog
The following dead simple program illustrates 3 ways to show message dialog in JOptionPane.

import javax.swing.*;
import java.awt.*;
class MessageDialogExample extends JFrame

    public MessageDialogExample()
    private void createAndShowGUI()
        setTitle("JSplitPane Example");
        // These display one after the other is closed
        JOptionPane.showMessageDialog(this,"This is message - 1");
        JOptionPane.showMessageDialog(this,"This is message - 2","This is title",JOptionPane.WARNING_MESSAGE);
        JOptionPane.showMessageDialog(this,"This is message - 3","This is title",JOptionPane.WARNING_MESSAGE,new ImageIcon("imageicon.png"));
    public static void main(String args[])
    SwingUtilities.invokeLater(new Runnable(){
            public void run()
            new MessageDialogExample();

showMessageDialog(): The three overloaded versions of these methods take in the parent component represented as this followed by the message to be displayed in the JOptionPane, it's title, type of message (warning or error or information etc) and image icon to be displayed.

Posted by at on

Tags: Swing Components,