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()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("JSplitPane Example");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
       
        // 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"));
       
       
        setSize(400,400);
        setVisible(true);   
    }
   
    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.

No comments: