JOptionPane showMessageDialog JavaExamples
Lately, I have been working with JOptionPane showMessageDialog, and I thought its a good I idea I share some information here to help those who are getting started with Java programming.
I will start with a simple JOptionPane showMessageDialog box that displays an individual message to the user once the code runs.My next posts I will post about JOptionPane showInputDialog.
I will start with a simple JOptionPane showMessageDialog box that displays an individual message to the user once the code runs.My next posts I will post about JOptionPane showInputDialog.
JOptionPane showMessageDialog Program Example
Execute below code.
[
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.UIManager;
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");
System.exit(0);
}
public static void main(String args[])
{
new SimpleJoptionMessageBox();
}
}
]
JOptionPane showMessageDialog Example Result
Code Explanation:
We have introduced Java Swing classes which are GUI widget toolkit for Java.Its used to provide users with graphical user interface.
In above class, i have extended JFrame class and used all the methods of the super class.
I have Super () is used to invoke all the immediate parent class constructor.
Class JFrame has methods for setting size, location which I have set to null meaning the window will be centered on the screen.
Others include setResizable( takes boolean).If true, the window can be resized and vice-versa.
UIManager.setLookAndFeel() sets all the swing components to take default Windows look and feel.
UIManager.setLookAndFeel() sets all the swing components to take default Windows look and feel.
Now back to our main topic of today, i.e., JOptionMessageDialog.
showMessageDialog()constructor takes several variables.
First you have to set the parent window for the message,i.e JFrame window.Adding this
keyword
to the showMessageDialog() constructor means that the message box will be placed in
the current
class window.
Message to be displayed comes next.
One can also set a custom icon for your message box.See below code.
JOptionPane showMessageDialog with Custom Title
Let take this JOptionPane to the next level and add a custom title.Adding custom title will force me to add type of JOptionPane.
Execute Below code.
[
import javax.swing.JOptionPane; import javax.swing.JFrame; import java.awt.Dimension; import javax.swing.UIManager; 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.INFORMATION_MESSAGE); System.exit(0); } public static void main(String args[]) { new SimpleJoptionMessageBox(); } }
]
Result:
JOptionPane.showMessageDialog(this,"Problem connecting to the server", "Server Error",JOptionPane.INFORMATION_MESSAGE); code adds tittle to JOPtionPane and sets the type of JOPtionPane to Information type.
There are others types of JOptionPane.JOptionPane with error message icon
To have error message icon just set JOptionPane.ERROR_MESSAGE I.eJOptionPane.showMessageDialog(this,"Problem connecting to the server","Server Error",JOptionPane.ERROR_MESSAGE);JOptionPane with warning icon
Add this code.
JOptionPane.showMessageDialog(this,"Problem connecting to the server","Server Error",JOptionPane.WARNING_MESSAGE);JOptionPane with Plain Message
Add this codeJOptionPane.showMessageDialog(this,"Problem connecting to the server", "Server Error",JOptionPane.PLAIN_MESSAGE);