Message Dialogs in Java (GUI)

Message dialogs provide information to the user. Message dialogs are created with the JOptionPane.showMessageDialog() method.

We call the static showMessageDialog() method of the JOptionPane class to create a message dialog. We provide the dialog’s parent, message text, title and a message type. The message type is one of the following constants :

  1. ERROR_MESSAGE
  2. WARNING_MESSAGE
  3. QUESTION_MESSAGE
  4. INFORMATION_MESSAGE

Methods Used :



  1. setLayout(…) : method help us to set the layout of the container, often a JPanel, to say FlowLayout, BorderLayout, GridLayout, null layout, or whatever layout we want to add on container.
  2. setBounds(…) : method is used to set the location and size of a components like JButton, and is only useful if null layout is used in JFrame.
  3. setVisible(…) : method is used to set Visiblity status of JFrame.
    1. setVisible(true) will set JFrame visible to user.
    2. setVisible(false) will set JFrame not visible to user.
  4. getSource() : An event object contains a reference to the component that generated the event. To extract that reference from the event object we use getSource() Method.
  5. add() : It is used to add components like JButton etc, to the container of JFrame.

    Below is the implementation of above discussed method to show Message Dialogs :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to show ERROR_MESSAGE dialog
    // in Java. Importing different Package.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      
    class Demo extends JFrame implements ActionListener 
    {
        // Declaration of object of JButton class.
        JButton b1;
          
        // Constructor of Demo class
        Demo()
        {
            // Setting layout as null of JFrame.
            this.setLayout(null);
              
            // Initialization of object of "JButton" class.
            b1 = new JButton("Button 1");
              
            // Setting Bounds of a JButton.
            b1.setBounds(130, 05, 100, 50);
              
            //"this" keyword in java refers to current object.
            // Adding JButton on JFrame.
            this.add(b1);
              
            // Adding Listener toJButton.
            b1.addActionListener(this);
        }
      
        // Override Method
        public void actionPerformed(ActionEvent evt)
        {
            if (evt.getSource() == b1) 
            {
                // Code To popup an ERROR_MESSAGE Dialog.
                JOptionPane.showMessageDialog(this, "Enter a valid Number"
                                       "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
      
    class MessageDialogs1 {
          
        // Driver code
        public static void main(String args[])
        {
            // Creating Object of demo class.
            Demo f = new Demo();
              
            // Setting Bounds of a Frame.
            f.setBounds(200, 200, 400, 300);
              
            // Setting Resizeable status of frame as false
            f.setResizable(false);
              
            // Setting Visible status of frame as true.
            f.setVisible(true);
        }
    }

    chevron_right

    
    

    Output :

    
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to show WARNING_MESSAGE dialog
    // in Java. Importing different Package.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      
    class Demo extends JFrame implements ActionListener 
    {
        // Declaration of object of JButton class.
        JButton b1;
          
        // Constructor of Demo class
        Demo()
        {
            // Setting layout as null of JFrame.
            this.setLayout(null);
              
            // Initialization of object of "JButton" class.
            b1 = new JButton("Button 2");
              
            // Setting Bounds of a JButton.
            b1.setBounds(130, 05, 100, 50);
              
            //"this" keyword in java refers to current object.
            // Adding JButton on JFrame.
            this.add(b1);
              
            // Adding Listener toJButton.
            b1.addActionListener(this);
        }
      
        // Override Method
        public void actionPerformed(ActionEvent evt)
        {
            if (evt.getSource() == b1) {
                  
                // Code To popup an WARNING_MESSAGE Dialog.
                JOptionPane.showMessageDialog(this, "Enter a valid String",
                                   "WARNING", JOptionPane.WARNING_MESSAGE);
            }
        }
    }
      
    class MessageDialogs2 {
          
        // Driver code
        public static void main(String args[])
        {
            // Creating Object of demo class.
            Demo f = new Demo();
              
            // Setting Bounds of a Frame.
            f.setBounds(200, 200, 400, 300);
              
            // Setting Resizeable status of frame as false
            f.setResizable(false);
              
            // Setting Visible status of frame as true.
            f.setVisible(true);
        }
    }

    chevron_right

    
    

    Output :

    
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to show QUESTION_MESSAGE
    // dialog in Java. Importing different Package.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      
    class Demo extends JFrame implements ActionListener 
    {
        // Declaration of object of JButton class.
        JButton b1;
          
        // Constructor of Demo class
        Demo()
        {
            // Setting layout as null of JFrame.
            this.setLayout(null);
              
            // Initialization of object of "JButton" class.
            b1 = new JButton("Button 3");
              
            // Setting Bounds of a JButton.
            b1.setBounds(130, 05, 100, 50);
              
            //"this" keyword in java refers to current object.
            // Adding JButton on JFrame.
            this.add(b1);
              
            // Adding Listener toJButton.
            b1.addActionListener(this);
        }
      
        // Override Method
        public void actionPerformed(ActionEvent evt)
        {
            if (evt.getSource() == b1) 
            {
                // Code TO popup a QUESTION_MESSAGE Dialog.
                JOptionPane.showMessageDialog(this, "Do you want to quit",
                                 "Question", JOptionPane.QUESTION_MESSAGE);
            }
        }
    }
      
    class MessageDialogs3 {
          
        // Driver code
        public static void main(String args[])
        {
            // Creating Object of demo class.
            Demo f = new Demo();
              
            // Setting Bounds of a Frame.
            f.setBounds(200, 200, 400, 300);
              
            // Setting Resizeable status of frame as false
            f.setResizable(false);
              
            // Setting Visible status of frame as true.
            f.setVisible(true);
        }
    }

    chevron_right

    
    

    Output :

    
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to show INFORMATION_MESSAGE
    // dialog in Java. Importing different Package.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      
    class Demo extends JFrame implements ActionListener 
    {
        // Declaration of object of JButton class.
        JButton b1;
          
        // Constructor of Demo class
        Demo()
        {
            // Setting layout as null of JFrame.
            this.setLayout(null);
              
            // Initialization of object of "JButton" class.
            b1 = new JButton("Button 4");
              
            // Setting Bounds of a JButton.
            b1.setBounds(130, 05, 100, 50);
              
            //"this" keyword in java refers to current object.
            // Adding JButton on JFrame.
            this.add(b1);
              
            // Adding Listener toJButton.
            b1.addActionListener(this);
        }
      
        // Override Method
        public void actionPerformed(ActionEvent evt)
        {
            if (evt.getSource() == b1) 
            {
                // Code To popup an INFORMATION_MESSAGE Dialog.
                JOptionPane.showMessageDialog(this, "You Pressed Button FOUR"
                                              "INFORMATION"
                                              JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }
      
    class MessageDialogs4 {
          
        // Driver code
        public static void main(String args[])
        {
              
            // Creating Object of demo class.
            Demo f = new Demo();
              
            // Setting Bounds of a Frame.
            f.setBounds(200, 200, 400, 300);
              
            // Setting Resizeable status of frame as false
            f.setResizable(false);
              
            // Setting Visible status of frame as true.
            f.setVisible(true);
        }
    }

    chevron_right

    
    

    Output :

    
    


    My Personal Notes arrow_drop_up

    A student of Btech Interested in coding and know languages like java C and many more

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.