Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

createDialog() - Creating Dialog Boxes Directly

This section provides a tutorial example on how to create and display dialog boxes directly with new instance of the JOptionPane class and the instance method of createDialog(). The instance method of getValue() should be used to get user selected/entered values in this case.

If you don't want to use show*Dialog() methods to create and display dialog boxes automatically, you can use the createDialog() method to create dialog boxes manually.

1. Use "myPane = new JOptionPane()" to create an empty option pane object, which represents content pane of the dialog box.

2. Use "myPane.setMessageType(type)" to set the message type on the option pane with a type code, INFORMATION_MESSAGE, WARNING_MESSAGE, ERROR_MESSAGE, PLAIN_MESSAGE, or QUESTION_MESSAGE.

3. Use "myPane.setMessage(message)" to set the message text on the option pane with a string.

4. Use "myPane.setOptionType(type)" to set the option type on the option pane with an option code, YES_NO_OPTION, YES_NO_CANCEL_OPTION, or OK_CANCEL_OPTION. This is not needed if you are setting your own options.

5. Use "myPane.setOptions(options)" to set options on the option pane with a string array. This will override options created by the option type.

6. Use "myPane.setInitialValue(default)" to set the default option on the option pane with a string.

7. Use "myDialog = myPane.createDialog(parent, title)" to create the final dialog box with a parent frame and a title string.

8. Use "myDialog.setVisible(true)" to set the final dialog box visible.

9. Use "answer = myPane.getValue()" to get the answer selected/entered by the user on the dialog box. The answer could be an integer if options are provided with an option type, or an string in other cases.

Here is an example program I wrote to test the createDialog() method:

/**
 * JOptionPanelCreateDialog.java
 * Copyright (c) 2009 by Dr. Herong Yang, http://www.herongyang.com/
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JOptionPanelCreateDialog 
   implements ActionListener {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new JOptionPanelCreateDialog()).test();
   }
   private void test() {
      myFrame = new JFrame("createDialog() Test");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container myPane = myFrame.getContentPane();
      JButton myButton = new JButton("Create");
      myButton.addActionListener(this);
      myPane.add(myButton);
      myFrame.pack();
      myFrame.setVisible(true);
   }
   public void actionPerformed(ActionEvent e) {
      String[] options = {"Java", "C++", "VB", "PHP", "Perl"};
      JOptionPane myPane = new JOptionPane();
      myPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
      myPane.setMessage("What language do you prefer?");
      myPane.setOptions(options);
      myPane.setInitialValue("PHP");
      JDialog myDialog = myPane.createDialog(
         myFrame, "Option Dialog Box");
      myDialog.setVisible(true);
      Object answer = myPane.getValue();
      System.out.println("Answer: "+answer);
   }
}

If you run this example, and click the Create button, you will see an input dialog box showing up like this:
Congirmation Dialog Box Input

Select "Perl" on the option dialog box. Repeat the test with the "Java" option button and the close dialog box icon. You will see some messages printed on the Java console window:

Answer: Perl
Answer: Java
Answer: null

Interesting notes about this tutorial example:

  • The dialog box created in this example is identical to the one created in the showOptionDialog() method in the JOptionPanelOptionDialog.java example.
  • But showOptionDialog() method returns integer indexes of the option array. But myPane.getValue() returns string elements of the option array.

Sample programs listed in this section have been tested with JDK 1.6.0.

Last update: 2009.

Sections in This Chapter

javax.swing.JOptionPane - Creating and Displaying Option Dialog Boxes

showMessageDialog() - Displaying Message Dialog Boxes

showConfirmDialog() - Displaying Confirmation Dialog Boxes

Receiving Inputs from Confirmation Dialog Boxes

showInputDialog() - Displaying Input Dialog Boxes

showOptionDialog() - Displaying Option Dialog Boxes

showInternal*Dialog() - Displaying Internal Dialog Boxes

createDialog() - Creating Dialog Boxes Directly

Dr. Herong Yang, updated in 2009
createDialog() - Creating Dialog Boxes Directly