19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

methods return a String that is entered from the text field in<br />

the input dialog. The last method returns an Object selected from<br />

a <strong>com</strong>bo box or a list. The input dialog displays a <strong>com</strong>bo box if<br />

there are fewer than twenty selection values, as shown in Figure<br />

38.14(b); it displays a list if there are twenty or more<br />

selection values, as shown in Figure 38.14(c).<br />

(a) text field (b) <strong>com</strong>bo box (c) list<br />

Figure 38.14<br />

(a) When creating an input dialog without specifying selection values,<br />

the input dialog displays a text field for data entry. (b) When<br />

creating an input dialog with selection values, the input dialog<br />

displays a <strong>com</strong>bo box if there are fewer than twenty selection values.<br />

(c) When creating an input dialog with selection values, the input<br />

dialog displays a list if there are twenty or more selection values.<br />

NOTE<br />

The showInputDialog method does not have the optionType<br />

parameter. The but<strong>to</strong>ns for input dialog are not<br />

configurable. The OK and Cancel but<strong>to</strong>ns are always used.<br />

38.6.4 Option Dialogs<br />

An option dialog allows you <strong>to</strong> create cus<strong>to</strong>m but<strong>to</strong>ns. You can<br />

create an option dialog using the following method:<br />

public static int showOptionDialog(Component parentComponent,<br />

Object message,<br />

String title,<br />

int optionType,<br />

int messageType,<br />

Icon icon,<br />

Object[] options,<br />

Object initialValue)<br />

The but<strong>to</strong>ns are specified using the options parameter. The<br />

initialValue parameter allows you <strong>to</strong> specify a but<strong>to</strong>n <strong>to</strong> receive<br />

initial focus. The showOptionDialog method returns an int value<br />

indicating the but<strong>to</strong>n that was activated. For example, here is<br />

the code that creates an option dialog, as shown in Figure 38.15:<br />

int value =<br />

JOptionPane.showOptionDialog(null, "Select a but<strong>to</strong>n",<br />

"Option Dialog", JOptionPane.DEFAULT_OPTION,<br />

JOptionPane.PLAIN_MESSAGE, null,<br />

new Object[]{"But<strong>to</strong>n 0", "But<strong>to</strong>n 1", "But<strong>to</strong>n 2"}, "But<strong>to</strong>n 1");<br />

Figure 38.15<br />

The option dialog displays the cus<strong>to</strong>m but<strong>to</strong>ns.<br />

22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!