Java Reference
In-Depth Information
LISTING 12.3
The Full Text of FormatChooser.java
1: import java.awt.*;
2: import java.awt.event.*;
3: import javax.swing.*;
4:
5: public class FormatChooser extends JFrame implements ItemListener {
6: String[] formats = { “(choose format)”, “Atom”, “RSS 0.92”,
7: “RSS 1.0”, “RSS 2.0” };
8: String[] descriptions = {
9: “Atom weblog and syndication format”,
10: “RSS syndication format 0.92 (Netscape)”,
11: “RSS/RDF syndication format 1.0 (RSS/RDF)”,
12: “RSS syndication format 2.0 (UserLand)”
13: };
14: JComboBox formatBox = new JComboBox();
15: JLabel descriptionLabel = new JLabel(“”);
16:
17: public FormatChooser() {
18: super(“Syndication Format”);
19: setSize(420, 150);
20: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21: setLayout(new BorderLayout());
22: for (int i = 0; i < formats.length; i++) {
23: formatBox.addItem(formats[i]);
24: }
25: formatBox.addItemListener(this);
26: add(BorderLayout.NORTH, formatBox);
27: add(BorderLayout.CENTER, descriptionLabel);
28: setVisible(true);
29: }
30:
31: public void itemStateChanged(ItemEvent event) {
32: int choice = formatBox.getSelectedIndex();
33: if (choice > 0) {
34: descriptionLabel.setText(descriptions[choice-1]);
35: }
36: }
37:
38: public Insets getInsets() {
39: return new Insets(50, 10, 10, 10);
40: }
41:
42: public static void main(String[] arguments) {
43: FormatChooser fc = new FormatChooser();
44: }
45: }
12
This application extends the combo box example from Day 9, “Working with Swing.”
Figure 12.3 shows this application.
 
Search WWH ::




Custom Search