Java Swing – JOptionPane showMessageDialog example
This is a review of the showMessageDialog()
method of JOptionPane
Class. This method is a quick and easy way to tell the user about something that has happened . The showMessageDialog()
can be called using the following combinations of parameters:
Component, Object
Component, Object, String, int
Component, Object, String, int, Icon
- Component – The first parameter is a component which determines the Frame in which the dialog is displayed; if null, or if the
parentComponent
has no Frame, a default Frame is used. - Object – The second parameter can be any objects. (In some older versions of Java you might get a compiler error when using primitive types directly).
- String – The third parameter is a String placed as the title of the message dialog window.
- int – The int that follows the String is the
MessageType
. The differentMessageTypes
forJOptionPane
, are:- ERROR_MESSAGE
- INFORMATION_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- PLAIN_MESSAGE
- Icon – The last parameter is an
Icon
that is displayed inside the dialog and overrides the defaultMessageType
icon.
1. Component & Object
The simplest way to use the message dialog. Example with Component
set to null and a String
as second argument:
package com.mkyong.messageDialog;
import javax.swing.JOptionPane;
public class SimpleDialog1 {
public static void main(String[] args){
JOptionPane.showMessageDialog(null, "Simple Information Message");
}
}
Output:
2. Component, Object, String & int
Adding some more information to the message dialog. Example with Component
set to null and a double
as second parameter:
package com.mkyong.messageDialog;
import javax.swing.JOptionPane;
public class SimpleDialog2a {
public static void main(String[] args){
JOptionPane.showMessageDialog(null, 8.9, "This is not an integer.", JOptionPane.PLAIN_MESSAGE);
}
}
Output:
Example of an Error Message (Component
set to null, String Object
):
package com.mkyong.messageDialog;
import javax.swing.JOptionPane;
public class SimpleDialog2b {
public static void main(String[] args){
JOptionPane.showMessageDialog(null, "Uh-oh!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
Output:
3. Component, Object, String, int & Icon
Make your message dialog “prettier”. Example with Icon
retrieved from a directory:
package messageDialog;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class SimpleDialog3a {
public static void main(String[] args){
ImageIcon icon = new ImageIcon("src/images/turtle64.png");
JOptionPane.showMessageDialog(null, "I like turtles.",
"Customized Dialog", JOptionPane.INFORMATION_MESSAGE, icon);
}
}
Output:
Example with Component
set to a frame
:
package com.mkyong.messageDialog;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Color;
public class MessageDialogInFrame extends JFrame{
public MessageDialogInFrame() {
getContentPane().setBackground(Color.DARK_GRAY);
setTitle("Message Dialog in Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
setSize(400, 300);
getContentPane().setLayout(null);
}
public static void main(String[] args){
ImageIcon icon = new ImageIcon("src/images/turtle64.png");
JOptionPane.showMessageDialog(new MessageDialogInFrame(),
"I appear as part of the frame!!", "Customized Dialog",
JOptionPane.INFORMATION_MESSAGE, icon);
}
}
Output:
4. A more advanced example
For this example we are passing a JPanel
as the Object parameter. The JPanel
is customized and has a JLabel
added to it. We are also manipulating the size of the OptionPane
using a call to UIManager
.
package com.mkyong.messageDialog;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
public class MessageDialogPanel {
public static void main(String[] args){
ImageIcon icon = new ImageIcon("src/images/turtle64.png");
JPanel panel = new JPanel();
panel.setBackground(new Color(102, 205, 170));
panel.setSize(new Dimension(200, 64));
panel.setLayout(null);
JLabel label = new JLabel("Turtles are awesome!!! :D");
label.setBounds(0, 0, 200, 64);
label.setFont(new Font("Arial", Font.BOLD, 11));
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
UIManager.put("OptionPane.minimumSize",new Dimension(300, 120));
JOptionPane.showMessageDialog(null, panel, "Customized Message Dialog", JOptionPane.PLAIN_MESSAGE, icon);
}
}
Output:
Hello there. Awesome tutorial but I was wondering, what is the function of null in the program?
The function of null is to nullify any kind of magic
How to okay roblox?
YEs
A noble question I have often asked myself, but I feel that the answer to this is contingent upon a separate idea: Should we okay Roblox?
no
Where is the example of a JOptionPane with a timer to self-close?
Thanks again!
how can I use dialog box if I have this set of code.
” Input”+ Count “=”+ Input2″=”.. It always says that the JOptionPane is error/ not suitable for this kind of code.