How to display custom buttons in a standard dialog box with JOptionPane. - Java Swing

Java examples for Swing:JOptionPane

Introduction

It asks the user his opinion about a JOptionPane.

Demo Code


import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

public class Main {
  public static void main(String[] args) {
    JComponent parentComponent = null;
    Object message = "How is JOptionPane?";
    String title = "JOptionPane Option Dialog";
    int messageType = JOptionPane.INFORMATION_MESSAGE;
    Icon icon = null;//from  w  w  w.j  a v  a  2s . co  m
    Object[] options = new String[] { "A", "B", "C" };
    Object initialOption = options[2];
    int response = JOptionPane.showOptionDialog(null, message, title,
        JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, icon,
        options, initialOption);
    switch (response) {
    case 0:
    case 1:
    case 2:
      System.out.println("You selected:" + options[response]);
      break;
    case JOptionPane.CLOSED_OPTION:
      System.out.println("You closed the dialog box.");
      break;
    default:
      System.out.println("I don't know what you did.");
    }

  }
}

Related Tutorials