Message Dialogs in Java (GUI)


Message dialogs contain information to the user. Message dialogs are developed with the JOptionPane.showMessageDialog() method.

To establish a message dialogue in an application. Utilize the showMessageDialog() function belonging to the JOptionPane Class. This method requires information such as a parent component reference, desired message content and title for your dialog box. Furthermore you can choose one of four constants (ERROR_MESSAGE WARNING MESSAGE etc.) for your specified type of message.

Methods Used

  • setLayout(…) − method enables us to set the layout of the container, often a JPanel, to any layout we wish to add to it, such as Flow Layout, Border Layout, Grid Layout or null.

  • setBounds(…) − method is only helpful if null layout is used in JFrame and is used to set the size and placement of components like JButton.

  • setVisible(…) − method is used to set the visibility of JFrame

setVisible(true)  // used to set JFrame visible to the users.
setVisible(false) // used to set JFrame not visible to the users.
  • getSource(..) − a reference to the element that caused the event to be contained in an event object. We utilize the getSource()method to obtain the reference from the event object.

  • add(…) − method used to add the component like JButton, etc. to the JFrame.

Examples of Message Dialogs

Program to show ERROR_MESSAGE

// Java program to show ERROR_MESSAGE dialog
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class example extends JFrame implements ActionListener {
   JButton b1;
	
   example() {
      this.setLayout(null);
      b1 = new JButton("Button 1");
      b1.setBounds(130, 05, 100, 50);
      this.add(b1);
		
      b1.addActionListener(this);
   }

   public void actionPerformed(ActionEvent evt) {
      if (evt.getSource() == b1) {
			
         JOptionPane.showMessageDialog(this, "Enter a valid Number","ERROR", JOptionPane.ERROR_MESSAGE);
      }
   }
}

class MessageDialogs1 {
   public static void main(String args[]) {
      example f = new example();
      f.setBounds(200, 200, 400, 300);
      f.setResizable(false);
      f.setVisible(true);
   }
}

Output

Example 2: Program to show WARNING_MESSAGE

// Java program to show WARNING_MESSAGE dialog
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class example extends JFrame implements ActionListener {
   JButton b1;
   example() {
      this.setLayout(null);
      b1 = new JButton("Button 2");
      b1.setBounds(130, 05, 100, 50);
		
      this.add(b1);
		
      b1.addActionListener(this);
   }

   public void actionPerformed(ActionEvent evt) {
      if (evt.getSource() == b1) {			
         JOptionPane.showMessageDialog(this, "Enter a valid String","WARNING", JOptionPane.WARNING_MESSAGE);
      }
   }
}

class MessageDialogs2 {
	
   public static void main(String args[]) {
      example f = new example();
      f.setBounds(200, 200, 400, 300);
      f.setResizable(false);
      f.setVisible(true);
   }
}

Output

Example 3: Program to show QUESTION_MESSAGE

// Java program to show QUESTION_MESSAGE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class example extends JFrame implements ActionListener {
   JButton b1;
   example() {
      this.setLayout(null);
      b1 = new JButton("Button 3");
      b1.setBounds(130, 05, 100, 50);
		
      this.add(b1);
		
      b1.addActionListener(this);
   }

   public void actionPerformed(ActionEvent evt) {
      if (evt.getSource() == b1) {
         JOptionPane.showMessageDialog(this, "Do you want to quit", "Question", JOptionPane.QUESTION_MESSAGE);
      }
   }
}

class MessageDialogs3 {
	
   public static void main(String args[]) {
      example f = new example();
      f.setBounds(200, 200, 400, 300);
      f.setResizable(false);
      f.setVisible(true);
   }
}

Output

Example 4: Program to show INFORMATION_MESSAGE

// Java program to show INFORMATION_MESSAGE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class example extends JFrame implements ActionListener {
   JButton b1;
	
   example() {
      this.setLayout(null);
      b1 = new JButton("Button 4");
      b1.setBounds(130, 05, 100, 50);
		
      this.add(b1);
		
      b1.addActionListener(this);
   }

   public void actionPerformed(ActionEvent evt) {
      if (evt.getSource() == b1) {
         JOptionPane.showMessageDialog(this, "You Pressed Button FOUR","INFORMATION",
         JOptionPane.INFORMATION_MESSAGE);
      }
   }
}

class MessageDialogs4 {
	
   public static void main(String args[]) {
      example f = new example();
      f.setBounds(200, 200, 400, 300);
      f.setResizable(false);
      f.setVisible(true);
   }
}

Output

Conclusion

To keep users informed in an efficient manner often entails presenting them with a clear cut UI element like a message dialog box. Calling on JOptionPane's showMessageDialog() feature makes this process easy to accomplish quickly and reliably every time user input is needed or required.The standard setting options available for messages include four different overrides meant for unique circumstances: ERROR_MESSSAGE ,WARNING_MESSSAGE ,QUESTION_MESSSAGE AND INFORMATION_MESSSAGE.Of course since we have additional five supplementary methods at our disposal namely namely -setLayout (),setBounds (),setVisible (),getSource (), and add() making tweaks of any message type with fine-grained detail in the UI is possible.

Updated on: 01-Aug-2023

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements