Input Dialog Boxes in Java Swing

Home  »  Java  »  Advance Java  »  Java Swing II  »  Input Dialog Boxes


An input dialog box asks a question and uses a text field to capture the response. The easiest way to create an input dialog is with a call to the showInputDialog(Component, Object) method. The arguments are the parent component and the string, component, or icon to display in the box. The input dialog method call returns a string represents the user's response.

The user can create an input dialog box with the showInputDialog(Component, Object, String, int) method.

The arguments the method takes are as follows:

  1. The title to display in the dialog box title bar.
  2. One of the five class variables describing the type of dialog box: ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, or WARNING_MESSAGE.

Example:

The following statement creates an input dialog box using the method:



Option Dialog Boxes

The most complex of the dialog boxes is the option dialog box, which combines the feature of all the other dialogs. It can be created with the showOptionDialog(Component, Object, String, int, int, Icon Object[]) method.

The arguments to this method are as follows:

  1. The parent component of the dialog.
  2. The text, icon or component to display.
  3. A string to display in the title bar.
  4. The type of box, using the class variable YES_NO_OPTION or YES_NO_CANCEL_OPTION or the literal 0 if other buttons will be used instead.
  5. The icon to display, using the class variables ERROR_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE, QUESTION_MESSAGE, or WARNING_MESSAGE, or the literal 0 if none of these should be used.
  6. An Icon object to display instead of one of the icons in the preceding argument.
  7. An array of object holding the components or the other objects that represent the choices in the dialog box, if YES_NO_OPTION or YES_NO_CANCEL_OPTION are not being used.
  8. The object representing the default selection if YES_NO_OPTION or YES_NO_CANCEL_OPTION are not being used.

The last two arguments enable the user to create a wide range of choices for the dialog box. The user can create an array of buttons, labels, text fields, or even a mixture of different components as an object array. These components are displayed using the flow layout manager.

Example:
The following example accepts information through dialog boxes from the user regarding the Site Title, Site Address and a choice of buttons that helps determine whether the site is a Personal, Commercial or an Unknow site.



C:\>jdk1.4\bin>javac InputDialogBoxes.java
C:\>jdk1.4\bin>java InputDialogBoxes


"Output of InputDialogBoxes.class"


Download Complete Program

  -  






Java Swing-II


JOptionPane Class Input Dialog Boxes JSliders
JProgressBars JTables JTabbedPane
JMenu JPopupMenu JToolBar
JFileChooser JScrollBar JScrollPane
JRootPane JTextPane JTree
JSplitPane JDesktopPane JEditorPane




1 comment: