The most complex of the dialog boxes is the option dialog box, which combines the features of all the other dialogs (Confirm Dialog Box, Input Dialog Box and Message Dialog Box). It can be created with the showOptionDialog(Component, Object, String, int, int, Icon, Object[], Object) method of JOptionPane class.

The arguments to this method are as follows:

  • The parent component of the dialog
  • The text, icon or component to display
  • A string to display in the title bar
  • The type of box, using the class constants YES_NO_OPTION or YES_NO_CANCEL_OPTION, or the literal 0 if other buttons will be used instead.
  • The icon to display, using the class constants ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE or WARNING_MESSAGE, or the literal 0 if none of these will be used.
  • An Icon object to display instead of one of the icons in the preceding argument
  • An array of objects holding the components or other objects that represent the choices in the dialog box. (if YES_NO_OPTION and YES_NO_CANCEL_OPTION are not being used.)
  • The object representing the default selection. (if YES_NO_OPTION and YES_NO_CANCEL_OPTION are not being selected.)

The following example creates an option dialog box that uses an array of JButton objects for the options in the box and color[3] as the default selection:

 JButton[] color = new JButton[4];
color[0] = new JButton("Blue");
color[1] = new JButton("Red");
color[2] = new JButton("Green");
color[3] = new JButton("Yellow");

int response = JOptionPane.showOptionDialog(null,
"Which color do you like?",