Show message and confirm dialog box

This section show you how to display several types of message box.

Show message and confirm dialog box

Show Message and Confirm Dialog Box - Swing Dialogs


This section show you how to display several types of message box. There are three types of message dialog box that you can use in your swing applications, example of each type of dialog boxes are provided here. When your run the program, it will display a frame with three buttons. Once you click on the first button then the simple message box will open which holds only "Ok" button as shown below:

Image 1

If you click on the second button then the confirm dialog box will open which asks for "Ok" and "Cancel".

Image 2

If you click on the "Ok" button then a message dialog box will open with message "You clicked on "Ok" button" like this
Image 4 
otherwise message dialog box will open with text "You clicked on "Cancel" button like this

Image 6
If you click on the third button from the main window or frame then a confirm message dialog box will open with three button i.e. the "Yes", "No" and "Cancel" like the following image:

For this purposes two methods have been used:

  • showMessageDialog():
    Above method shows a simple message dialog box which holds only one button i.e. "Ok" button. This method takes four arguments in which, first is the parent object name, second is the message as string, third is the title of the message dialog box as string and the last is the type of the message dialog box.
  • showConfirmDialog():
    Above method asks from the user by displaying a message dialog box which contains more than one button. Depending on the parameter passed it can be "Ok" and "Cancel" or  "Yes", "No" and "Cancel". This method returns the integer value.

Here is the code of the program:

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

public class ShowMessageDialog{
  JButton button;
  public static void main(String[] args){
  ShowMessageDialog md = new ShowMessageDialog();

  public ShowMessageDialog(){
  JFrame frame = new JFrame("Message Dialog Box");
  button = new JButton("Show simple message dialog box");
  button.addActionListener(new MyAction());
  JPanel panel = new JPanel();
  button = new JButton("Show \"Ok/Cancel\" message 
dialog box"
  button.addActionListener(new MyAction());
  button = new JButton("Show \"Yes/No/Cancel\" dialog box");
  button.addActionListener(new MyAction());

  public class MyAction implements ActionListener{
  public void actionPerformed(ActionEvent ae){
  String str = ae.getActionCommand();
  if(str.equals("Show simple message dialog box")){
  JOptionPane.showMessageDialog(null, "This is the simple message 
dialog box."
  else if(str.equals("Show \"Ok/Cancel\" message dialog box")){
  if(JOptionPane.showConfirmDialog(null, "This is the \"Ok/Cancel\" 
message dialog box."
"", JOptionPane.OK_CANCEL_OPTION== 0)
  JOptionPane.showMessageDialog(null, "You clicked on \"Ok\" button",
  JOptionPane.showMessageDialog(null, "You clicked on \"Cancel\" button",
  else if(str.equals("Show \"Yes/No/Cancel\" dialog box")){
  JOptionPane.showConfirmDialog(null, "This is the \"Yes/No/Cancel\"
 message dialog box."

Download this example.


  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