Java Message Box

In this section we will use JOptionPane class to display the message Dialog box.

Java Message Box

Java Message Box

In this tutorials you will learn about how to create Message Box in Java. Java API provide a class to create a message Box in java. By message box we mean a box in which message is displayed or a box from which input provided by the user can be retrieved. A message box is meant to carry a temporary information or message apart from the main swing application. Most of the message box is used for error message. Using JOptionPane class you can create a message box. Here is the code that creates Message box and shows it:

JOptionPane.showMessageDialog(frame, "This is a Message Box.");

This statement will display a message with message as "This is a Message Box.".

There are  several feature JOptionPane like customizing icon, customizing the component the dialog display and specify where message box should appear on screen.

JOptionPane icon let you decide which icon you want to display. You can use custom icon, or no icon, or any of the four JOptionPane standard icon. The two most useful method of showxxxdialog method are as follows :

  •  showMessageDiaolog : The showMessageDialog method will display the one button dialog method.
  •  showOptionDialog :  This method display a variety of button with button text. and contain a standard text message.
  • showConfirmDialog : It will ask for confirmation of something with Yes/No.
  • showInputDialog : This will display a message box that accept input from user from text field or combo box.

showMessageDialog : Display a message with one button which is labeled "OK". You can specify the message, title, and icon according to your wish. Here are some example using showMessageDialog :

JOptionPane.showMessageDialog(frame," Message Box Demo.");

JOptionPane.showMessageDialog(frame,"Message Box Demo.","Warning",JOptionPane.WARNING_MESSAGE);

  

JOptionPane.showMessageDialog(frame,"Message Box Demo","Error Message Box",JOptionPane.ERROR_MESSAGE);

showOptionDialog : Displays a Message with the specified buttons, icons, message, title, and so on. By the help of this message, you can change the text that appears on the buttons of the message Box.

Object[] options = {"Yes","No","ask later"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like to save it",null, JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[2]);

JOptionPane () :  Creates a JOptionPane with the specified buttons, icons, message, title, and so on.

Example : We create a program which will display the message box using a swing. First creating a swing application in which we created a button. When you click on the button then message box will pop up and display a message.

import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.event.*;

public class MessageBox {
	JFrame frame;

	public static void main(String[] args) {
		MessageBox db = new MessageBox();
	}

	public MessageBox() {
		frame = new JFrame("Show Message Box");
		JButton button = new JButton("Click");
		button.setAlignmentX((float) 100.00);

		button.addActionListener(new MyAction());
		frame.add(button);
		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public class MyAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(frame, "Message Box");
		}
	}
}

Output from the program :

When you click on button then a message box will appear as below:

Download Source Code

Tutorials

  1. Display Image in Java
  2. Show Coordinates
  3. What is Java Swing?
  4. Creating a Frame
  5. Setting an Icon for a Frame in Java
  6. Show Dialog Box in Java
  7. Show message and confirm dialog box
  8. Show input dialog box
  9. Changing the Label of a JButton Component in Java
  10. Setting Multi-Line label on the Button
  11. Setting icon on the button in Java
  12. Making a Frame Non Resizable in Java
  13. Remove Minimize and Maximize Button of Frame in Java
  14. Removing the Title Bar of a Frame
  15. Setting Bounds for a maximized frame
  16. Iconifying and Maximizing a frame in Java
  17. Making a component drag gable in java
  18. Create a ToolBar in Java
  19. Animating Images in Java Application
  20. Drawing with Color in Java
  21. Drawing with Gradient Color in Java
  22. Adding a Rollover and Pressed Icon to a JButton Component in Java
  23. Creating Check Box in Java Swing
  24. Customize the Icon in a JCheckBox Component of Java Swing
  25. Create a JComboBox Component in Java
  26. Combo Box operation in Java Swing
  27. Create a JRadioButton Component in Java
  28. Selecting a Radio Button component in Java
  29. Create a JList Component in Java
  30. Setting Tool Tip Text for items in a JList Component
  31. Setting the Dimensions of an Item in a JList Component in Java
  32. Create a JSpinner Component in Java
  33. Show Time in JSpinner Component
  34. Disabling Keyboard Editing in a JSpinner Component
  35. Limiting the Values in a Number JSpinner Component
  36. JSlider Component of Java Swing
  37. Progress Bar in Java Swing
  38. Create menus and submenus in Java
  39. Crate a Popup Menu in Java
  40. Create a Popup Menus with Nested Menus in Java