JOptionPane showMessageDialog JavaExamples

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.


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

JOptionPane showMessageDialog JavaExamples

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.

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.INFORMATION_MESSAGE



 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.e
JOptionPane.showMessageDialog(this,
"Problem connecting to the server","Server Error",JOptionPane.ERROR_MESSAGE);
JOptionPane.ERROR_MESSAGE

JOptionPane with warning icon

Add this code.

JOptionPane.showMessageDialog(this,"Problem connecting to the server",
"Server Error",JOptionPane.WARNING_MESSAGE);
JOptionPane-warning-message

JOptionPane with Plain Message

Add this code
JOptionPane.showMessageDialog(this,"Problem connecting to the server", "Server Error",JOptionPane.PLAIN_MESSAGE);
JOptionPane.PLAIN_MESSAGE

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: JOptionPane showMessageDialog JavaExamples
JOptionPane showMessageDialog JavaExamples
JOptionPane showMessageDialog JavaExamples
https://4.bp.blogspot.com/-lgDjoMC2hTU/WJ7BgVQXkZI/AAAAAAAAG2o/ieSBHwuc_3Ii2nxstg8YwwdOfNan7ZIxwCLcB/s200/JOptionPane-showMessageDialogBox-min.PNG
https://4.bp.blogspot.com/-lgDjoMC2hTU/WJ7BgVQXkZI/AAAAAAAAG2o/ieSBHwuc_3Ii2nxstg8YwwdOfNan7ZIxwCLcB/s72-c/JOptionPane-showMessageDialogBox-min.PNG
Java Codes
http://www.download-all.net/2017/02/joptionpane-showmessagedialog.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/02/joptionpane-showmessagedialog.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy