Java Swing Tutorials - Herong's Tutorial Examples - Version 4.11, by Dr. Herong Yang
createDialog() - Creating Dialog Boxes Directly
This section provides a tutorial example on how to use the static method, showInputDialog(), to create and display input dialog boxes to take text string input from the user.
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:
/* JOptionPaneCreateDialog.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JOptionPaneCreateDialog implements ActionListener { JFrame myFrame = null; public static void main(String[] a) { (new JOptionPaneCreateDialog()).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:
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:
Sample programs listed in this section have been tested with JDK 1.6.0 to 1.8.0.
Last update: 2014.
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
JRadioButton - Swing Radio Button Class
JTextField - Swing Text Field Class
Menu Bar, Menus, Menu Items and Listeners
Creating Internal Frames inside the Main Frame
Layout of Components in a Container
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
JEditorPane - The Editor Pane Class