JOptionPane showMessageDialog Java Examples Part 2

See also JOptionPane showMessageDialog Java Examples Part 1 Its very important to note that you can add a custom icon to JOptioPane...

JOptionPane-with-custom-icon

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.
[
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)

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 Java Examples Part 2
JOptionPane showMessageDialog Java Examples Part 2
https://4.bp.blogspot.com/-RoXzjwuvBaM/WJ-ajlbBkVI/AAAAAAAAG3o/9rtPCdGuSSQCZTl24GsaxPzi1eczEqQ2QCEw/s320/JOptionPane-with-custom-icon.PNG
https://4.bp.blogspot.com/-RoXzjwuvBaM/WJ-ajlbBkVI/AAAAAAAAG3o/9rtPCdGuSSQCZTl24GsaxPzi1eczEqQ2QCEw/s72-c/JOptionPane-with-custom-icon.PNG
Java Codes
http://www.download-all.net/2017/02/joptionpane-showmessagedialog-java.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/02/joptionpane-showmessagedialog-java.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