Getting the Results

Now that we’ve seen how to create all sorts of useful dialog boxes, it’s time to take a look at how to retrieve information about the user’s interaction with the dialog. Table 10-7 showed the return types of the various methods. Here’s a quick summary of what the returned values mean.

Input Dialogs

The versions that do not take an array of selection values return a String. This is the data entered by the user. The methods that do take an array of selection values return an Object reflecting the selected option. It’s up to the L&F to determine the component used for presenting the options. Typically, a JComboBox is used if there are fewer than 20 choices, and a JList is used if there are 20 or more.[14] In any case, if the user presses the Cancel button, null is returned.

Confirm Dialogs

These methods return an int reflecting the button pressed by the user. The possible values are: YES_OPTION, NO_OPTION, CANCEL_OPTION, and OK_OPTION. CLOSED_OPTION is returned if the user closes the window without selecting anything.

Message Dialogs

These methods have void return types because they do not request a user response.

Option Dialogs

If no options are specified, this method returns one of the constant values YES_OPTION, NO_OPTION, CANCEL_OPTION, and OK_OPTION. If options are explicitly defined, the return value gives the index to the array of options that matches the button selected by the user. CLOSED_OPTION is returned if the user closes the window without selecting anything. ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.