. Advertisement .
..3..
. Advertisement .
..4..
Swing – the default GUI toolkit – can help you create a message box in Java. Find out how with examples below.
Message Box In Java
Dialogs In Swing
In Swing, you can create a message box with a dialog window. It is a sub-window independent from the main Application Window. The most common application of this window is to show temporary notice, such as a warning or error message. But you can also add anything compatible with the main Application, including directory trees and images.
Every dialog window in Swing is dependent on a Frame component. When this Frame no longer exists, the dependent Dialogs will also be destroyed.
Swing actually comes with more than one component class you can use to create dialogs. For instance, the JOptionPane class is a common choice for simple dialogs. They can also be created with the JFileChooser and JColorChooser classes.
There are some other options for more specific purposes. The ProgressMonitor class can display an operation’s progress, the Printing API can supply a standard print dialog, while the JDialog class allows you to customize your dialog directly.
Create A Message Box With JOptionPane
The JOptionPane class is the recommended starting point if this is the first time you want to create a message box in Java. It is simple to use and able to generate standard dialogs you can find on other applications.
This class offers a large number of methods, which may make it too complex for beginners. But most people only need to rely on some simple static methods below:
- showConfirmDialog
- showInputDialog
- showMessageDialog (the method you can use to create a message box)
- showOptionDialog
All of them accept these parameters:
parentComponent: the Component that will become the parent of this message box. Generally, your dialog box will be placed right under this component. You can set it to null, which will put the dialog in the center of the screen and use a default Frame as its parent component.
message: the message you want to place on the message box. Typically, this is a String literal. But you can also use an array of objects.
messageType: the style of your message box. The Look and Feel manager will change the layout and icon of your message box according to this. Its possible values include:
- INFORMATION_MESSAGE
- ERROR_MESSAGE
- PLAIN_MESSAGE
- QUESTION_MESSAGE
- WARNING_MESSAGE
title: the window title of your message box.
The following example uses the showMessageDialog()
method and those parameters to create a message box:
import javax.swing.*;
class DialogueBoxPopUp {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,
"Hi! Welcome to ITTutoria.net!",
"ITTutoria",
JOptionPane.INFORMATION_MESSAGE);
}
}
......... ADVERTISEMENT .........
..8..
The snippet above provides the method with the parent component (null), the message (“Hi! Welcome to ITTutoria.net!”), the title (“ITTutoria”), and the message’s style (INFORMATION_MESSAGE).
All dialogs created by JOptionPane are modal. The method above will block the caller until the user completes their interaction.
You can set messageType to a different value if your message carries a different meaning. It will change the appearance of the dialog.
For instance, this is how you can create an error message with showMessageDialog:
JOptionPane.showMessageDialog(null,
"An error has occurred!",
"ITTutoria",
JOptionPane.ERROR_MESSAGE);
......... ADVERTISEMENT .........
..8..
Summary
You can create a simple message box in Java with the showMessageDialog()
method of the JOptionPane class. This class also provides other methods for other styles of dialog boxes. Remember that Swing also allows you to create more complicated graphics, such as triangles. Check out this guide to find out how.
Leave a comment