See also JOptionPane showMessageDialog Java Examples Part 1 Its very important to note that you can add a custom icon to JOptioPane...
Its very important to note that you can add a custom icon to JOptioPane.showMessageDialog .This example will show you how to add custom icon.
JOptionPane with custom Icon
Code
[
import javax.swing.JOptionPane; import javax.swing.JFrame; import java.awt.Dimension; import javax.swing.UIManager; import javax.swing.ImageIcon; public class SimpleJoptionMessageBox extends JFrame { //class constructor public SimpleJoptionMessageBox() { super("Simple JoptionMessage Box Window"); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } setPreferredSize(new Dimension(400, 300)); pack(); setLocationRelativeTo(null); setVisible(true); JOptionPane.showMessageDialog(this,"Problem connecting to the server", "Server Error",JOptionPane.PLAIN_MESSAGE, new ImageIcon(SimpleJoptionMessageBox.class.getResource("server.jpg"))); System.exit(0); } public static void main(String args[]) { new SimpleJoptionMessageBox(); } }
]
JOptionPane.showMessageDialog(this,"Problem connecting to the server",
"Server Error",JOptionPane.PLAIN_MESSAGE,
new ImageIcon(SimpleJoptionMessageBox.class.getResource("server.jpg")));
Above code adds the custom icon that has been placed inside the same package as the class.
I real world programming,a JOptionPane is added when a action is peformed like a JButton Action Listener.
Lets think of a real scenario.Lets say when a user clicks button and there is a empty JTextField that need to be filled .See below example.
[
When button is clicked,method if(name.isEmpty()) checks if the JTextField has any input.Incase true is returned,JOptionPane message dialog box pops up requesting user to enter firstname
"Server Error",JOptionPane.PLAIN_MESSAGE,
new ImageIcon(SimpleJoptionMessageBox.class.getResource("server.jpg")));
Above code adds the custom icon that has been placed inside the same package as the class.
I real world programming,a JOptionPane is added when a action is peformed like a JButton Action Listener.
Lets think of a real scenario.Lets say when a user clicks button and there is a empty JTextField that need to be filled .See below example.
[
import javax.swing.JOptionPane; import javax.swing.JFrame; import java.awt.Dimension; import javax.swing.UIManager; import javax.swing.ImageIcon; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SimpleJoptionMessageBox extends JFrame { //class constructor public SimpleJoptionMessageBox() { super("Simple JoptionMessage Box Window"); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } setLayout(new FlowLayout()); JTextField field=new JTextField(); add(field); JButton button=new JButton("Save"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String name=field.getText(); if(name.isEmpty()) { JOptionPane.showMessageDialog(null, "Enter Firstname","Input",
JOptionPane.INFORMATION_MESSAGE); } } }); add(button); setPreferredSize(new Dimension(400, 300)); pack(); setLocationRelativeTo(null); setVisible(true); } public static void main(String args[]) { new SimpleJoptionMessageBox(); } }]
When button is clicked,method if(name.isEmpty()) checks if the JTextField has any input.Incase true is returned,JOptionPane message dialog box pops up requesting user to enter firstname
showMessageDialog method types
showMessageDialog box can takes different variables.Below are some of the examples
- showMessageDialog(Component parentComponent, Object message)
- showMessageDialog(Component parentComponent, Object message, String title, int messageType)
- showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)