Show a confirmation dialog box - Java Swing

Java examples for Swing:JOptionPane

Description

Show a confirmation dialog box

Demo Code

import javax.swing.JOptionPane;

public class Main {
  public static void main(String[] args) {
    int response = JOptionPane.showConfirmDialog(null,
        "Are you sure you want to save the changes?", "Confirm Save Changes",
        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

    switch (response) {
    case JOptionPane.YES_OPTION:
      System.out.println("You chose yes");
      break;//from ww w.  j  av a  2s.c o m
    case JOptionPane.NO_OPTION:
      System.out.println("You chose no");
      break;
    case JOptionPane.CANCEL_OPTION:
      System.out.println("You chose cancel");
      break;
    case JOptionPane.CLOSED_OPTION:
      System.out.println("You closed the dialog box.");
      break;
    default:
      System.out.println("I do not know what you did ");
    }
  }
}

Related Tutorials