Java Reference
In-Depth Information
The final two arguments offer a wide range of possibilities for the dialog box. You can
create an array of strings that holds the text of each button to display on the dialog box.
These components are displayed using the flow layout manager.
The following example creates an option dialog box that uses an array of String objects
for the options in the box and the gender[2] element as the default selection:
String[] gender = { “Male”, “Female”,
“None of Your Business” };
int response = JOptionPane.showOptionDialog(null,
“What is your gender?”,
“Gender”,
0,
JOptionPane.INFORMATION_MESSAGE,
null,
gender,
gender[2]);
Figure 10.9 shows the resulting dialog box.
FIGURE 10.9
An option dialog
box.
Using Dialog Boxes
The next project shows a series of dialog boxes in a working program. The FeedInfo
application uses dialog boxes to get information from the user; that information is then
placed into text fields in the application's main window.
Enter Listing 10.1 and compile the result.
LISTING 10.1
The Full Text of FeedInfo.java
1: import java.awt.GridLayout;
2: import java.awt.event.*;
3: import javax.swing.*;
4:
5: public class FeedInfo extends JFrame {
6: private JLabel nameLabel = new JLabel(“Name: “,
7: SwingConstants.RIGHT);
8: private JTextField name;
9: private JLabel urlLabel = new JLabel(“URL: “,
Search WWH ::




Custom Search