JOptionPane: showMessageDialog
This tutorial describes how to create and display a simple dialog box that displays a message with the JOptionPane.showMessageDialog() method. The simplest message that can be created is achievable by using the class javax.swing.JOptionPane.showMessageDialog displays a window with a single "OK" button, you can specify the message, the title and the icon with the static method:
showMessageDialog(Component parent, Object message, String title, int typeMessage, Icon icon), or:
- parent: is the parent Component (window or panels);
- message: is the string message that will be displayed in the JOptionPane;
- title: is the title of type string.
- typeMessage: this is the type of message of type entire: INFORMATION_MESSAGE, WARNING_MESSAGE, ERROR_MESSAGE and PLAIN_MESSAGE;
- icon: is the icon that replaces the default icon;
Here is a practical example to test the showMessageDialog:
import javax.swing.JOptionPane;
public class MessageDialog {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,
"Database has been updated",
"Update complete",
JOptionPane.INFORMATION_MESSAGE,
null);
}
}
Result after execution:
Unlike other JOptionPane methods that are functions and return a String or int type, showMessageDialog is a procedure that returns nothing.
We put null in the "parent" argument because the dialog doesn't have a parent. If you don't want to insert an icon, put null in the icon argument box. The compiler displays the default icon based on the message type.
References
Java Doc: showMessageDialog method
java2s: Using JOptionPane to Display a Message: JOptionPane Dialog
Unlike other JOptionPane methods that are functions and return a String or int type, showMessageDialog is a procedure that returns nothing.
We put null in the "parent" argument because the dialog doesn't have a parent. If you don't want to insert an icon, put null in the icon argument box. The compiler displays the default icon based on the message type.
References
Java Doc: showMessageDialog method
java2s: Using JOptionPane to Display a Message: JOptionPane Dialog