Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / examples / gnu / classpath / examples / swing / ComboBoxDemo.java
blob52431cb5d53e9d3dd304ad10089813e0e6f2be1c
1 /* ComboBoxDemo.java -- An example showing various combo boxes in Swing.
2 Copyright (C) 2005, Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
23 package gnu.classpath.examples.swing;
25 import java.awt.BorderLayout;
26 import java.awt.Color;
27 import java.awt.Component;
28 import java.awt.Dimension;
29 import java.awt.Font;
30 import java.awt.GridLayout;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
34 import javax.swing.BorderFactory;
35 import javax.swing.DefaultListCellRenderer;
36 import javax.swing.Icon;
37 import javax.swing.JButton;
38 import javax.swing.JCheckBox;
39 import javax.swing.JComboBox;
40 import javax.swing.JFrame;
41 import javax.swing.JList;
42 import javax.swing.JPanel;
43 import javax.swing.UIManager;
44 import javax.swing.plaf.metal.MetalIconFactory;
46 /**
47 * A simple demo showing various combo boxes in different states.
49 public class ComboBoxDemo
50 extends JFrame
51 implements ActionListener
54 class CustomCellRenderer extends DefaultListCellRenderer
56 public Component getListCellRendererComponent(JList list,
57 Object value,
58 int index,
59 boolean isSelected,
60 boolean cellHasFocus)
62 DefaultListCellRenderer result = (DefaultListCellRenderer)
63 super.getListCellRendererComponent(list, value, index, isSelected,
64 cellHasFocus);
65 Icon icon = (Icon) value;
66 result.setIcon(icon);
67 result.setText("Index = " + index);
68 return result;
72 private JCheckBox comboState1;
73 private JComboBox combo1;
74 private JComboBox combo2;
76 private JCheckBox comboState2;
77 private JComboBox combo3;
78 private JComboBox combo4;
80 private JCheckBox comboState3;
81 private JComboBox combo5;
82 private JComboBox combo6;
84 private JCheckBox comboState4;
85 private JComboBox combo7;
86 private JComboBox combo8;
88 private JCheckBox comboState5;
89 private JComboBox combo9;
90 private JComboBox combo10;
92 private JCheckBox comboState6;
93 private JComboBox combo11;
94 private JComboBox combo12;
96 /**
97 * Creates a new demo instance.
99 * @param title the frame title.
101 public ComboBoxDemo(String title)
103 super(title);
104 JPanel content = createContent();
105 JPanel closePanel = new JPanel();
106 JButton closeButton = new JButton("Close");
107 closeButton.setActionCommand("CLOSE");
108 closeButton.addActionListener(this);
109 closePanel.add(closeButton);
110 content.add(closePanel, BorderLayout.SOUTH);
111 getContentPane().add(content);
115 * Returns a panel with the demo content. The panel
116 * uses a BorderLayout(), and the BorderLayout.SOUTH area
117 * is empty, to allow callers to add controls to the
118 * bottom of the panel if they want to (a close button is
119 * added if this demo is being run as a standalone demo).
121 JPanel createContent()
123 JPanel content = new JPanel(new BorderLayout());
124 JPanel panel = new JPanel(new GridLayout(6, 1));
125 panel.add(createPanel1());
126 panel.add(createPanel2());
127 panel.add(createPanel3());
128 panel.add(createPanel4());
129 panel.add(createPanel5());
130 panel.add(createPanel6());
131 content.add(panel);
132 return content;
135 private JPanel createPanel1()
137 JPanel panel = new JPanel(new BorderLayout());
138 this.comboState1 = new JCheckBox("Enabled", true);
139 this.comboState1.setActionCommand("COMBO_STATE1");
140 this.comboState1.addActionListener(this);
141 panel.add(this.comboState1, BorderLayout.EAST);
143 JPanel controlPanel = new JPanel();
144 controlPanel.setBorder(BorderFactory.createTitledBorder("Regular: "));
145 this.combo1 = new JComboBox(new Object[] {"Australia", "New Zealand",
146 "England"});
148 this.combo2 = new JComboBox(new Object[] {"Australia", "New Zealand",
149 "England"});
150 this.combo2.setEditable(true);
152 controlPanel.add(combo1);
153 controlPanel.add(combo2);
155 panel.add(controlPanel);
157 return panel;
160 private JPanel createPanel2()
162 JPanel panel = new JPanel(new BorderLayout());
163 this.comboState2 = new JCheckBox("Enabled", true);
164 this.comboState2.setActionCommand("COMBO_STATE2");
165 this.comboState2.addActionListener(this);
166 panel.add(this.comboState2, BorderLayout.EAST);
168 JPanel controlPanel = new JPanel();
169 controlPanel.setBorder(BorderFactory.createTitledBorder("Large Font: "));
170 this.combo3 = new JComboBox(new Object[] {"Australia", "New Zealand",
171 "England"});
172 this.combo3.setFont(new Font("Dialog", Font.PLAIN, 20));
174 this.combo4 = new JComboBox(new Object[] {"Australia", "New Zealand",
175 "England"});
176 this.combo4.setEditable(true);
177 this.combo4.setFont(new Font("Dialog", Font.PLAIN, 20));
179 controlPanel.add(combo3);
180 controlPanel.add(combo4);
182 panel.add(controlPanel);
184 return panel;
187 private JPanel createPanel3()
189 JPanel panel = new JPanel(new BorderLayout());
190 this.comboState3 = new JCheckBox("Enabled", true);
191 this.comboState3.setActionCommand("COMBO_STATE3");
192 this.comboState3.addActionListener(this);
193 panel.add(this.comboState3, BorderLayout.EAST);
195 JPanel controlPanel = new JPanel();
196 controlPanel.setBorder(BorderFactory.createTitledBorder("Colored Background: "));
197 this.combo5 = new JComboBox(new Object[] {"Australia", "New Zealand",
198 "England"});
199 this.combo5.setBackground(Color.yellow);
201 this.combo6 = new JComboBox(new Object[] {"Australia", "New Zealand",
202 "England"});
203 this.combo6.setEditable(true);
204 this.combo6.setBackground(Color.yellow);
206 controlPanel.add(combo5);
207 controlPanel.add(combo6);
209 panel.add(controlPanel);
211 return panel;
215 * This panel contains combo boxes that are empty.
217 * @return A panel.
219 private JPanel createPanel4()
221 JPanel panel = new JPanel(new BorderLayout());
222 this.comboState4 = new JCheckBox("Enabled", true);
223 this.comboState4.setActionCommand("COMBO_STATE4");
224 this.comboState4.addActionListener(this);
225 panel.add(this.comboState4, BorderLayout.EAST);
227 JPanel controlPanel = new JPanel();
228 controlPanel.setBorder(BorderFactory.createTitledBorder("Empty: "));
229 this.combo7 = new JComboBox();
230 this.combo8 = new JComboBox();
231 this.combo8.setEditable(true);
233 controlPanel.add(combo7);
234 controlPanel.add(combo8);
236 panel.add(controlPanel);
238 return panel;
242 * This panel contains combo boxes that are narrow but contain long text
243 * items.
245 * @return A panel.
247 private JPanel createPanel5()
249 JPanel panel = new JPanel(new BorderLayout());
250 this.comboState5 = new JCheckBox("Enabled", true);
251 this.comboState5.setActionCommand("COMBO_STATE5");
252 this.comboState5.addActionListener(this);
253 panel.add(this.comboState5, BorderLayout.EAST);
255 JPanel controlPanel = new JPanel();
256 controlPanel.setBorder(BorderFactory.createTitledBorder("Narrow: "));
257 this.combo9 = new JComboBox(new Object[] {
258 "A really long item that will be truncated when displayed"});
259 this.combo9.setPreferredSize(new Dimension(100, 30));
260 this.combo10 = new JComboBox(new Object[] {
261 "A really long item that will be truncated when displayed"});
262 this.combo10.setPreferredSize(new Dimension(100, 30));
263 this.combo10.setEditable(true);
265 controlPanel.add(combo9);
266 controlPanel.add(combo10);
268 panel.add(controlPanel);
270 return panel;
274 * This panel contains combo boxes with a custom renderer.
276 * @return A panel.
278 private JPanel createPanel6()
280 JPanel panel = new JPanel(new BorderLayout());
281 this.comboState6 = new JCheckBox("Enabled", true);
282 this.comboState6.setActionCommand("COMBO_STATE6");
283 this.comboState6.addActionListener(this);
284 panel.add(this.comboState6, BorderLayout.EAST);
286 JPanel controlPanel = new JPanel();
287 controlPanel.setBorder(BorderFactory.createTitledBorder("Custom Renderer: "));
288 this.combo11 = new JComboBox(new Object[] {
289 MetalIconFactory.getFileChooserHomeFolderIcon(),
290 MetalIconFactory.getFileChooserNewFolderIcon()});
291 this.combo11.setPreferredSize(new Dimension(100, 30));
292 this.combo11.setRenderer(new CustomCellRenderer());
293 this.combo12 = new JComboBox(new Object[] {
294 MetalIconFactory.getFileChooserHomeFolderIcon(),
295 MetalIconFactory.getFileChooserNewFolderIcon()});
296 this.combo12.setPreferredSize(new Dimension(100, 30));
297 this.combo12.setRenderer(new CustomCellRenderer());
298 this.combo12.setEditable(true);
300 controlPanel.add(combo11);
301 controlPanel.add(combo12);
303 panel.add(controlPanel);
305 return panel;
308 public void actionPerformed(ActionEvent e)
310 if (e.getActionCommand().equals("COMBO_STATE1"))
312 combo1.setEnabled(comboState1.isSelected());
313 combo2.setEnabled(comboState1.isSelected());
315 else if (e.getActionCommand().equals("COMBO_STATE2"))
317 combo3.setEnabled(comboState2.isSelected());
318 combo4.setEnabled(comboState2.isSelected());
320 else if (e.getActionCommand().equals("COMBO_STATE3"))
322 combo5.setEnabled(comboState3.isSelected());
323 combo6.setEnabled(comboState3.isSelected());
325 else if (e.getActionCommand().equals("COMBO_STATE4"))
327 combo7.setEnabled(comboState4.isSelected());
328 combo8.setEnabled(comboState4.isSelected());
330 else if (e.getActionCommand().equals("COMBO_STATE5"))
332 combo9.setEnabled(comboState5.isSelected());
333 combo10.setEnabled(comboState5.isSelected());
335 else if (e.getActionCommand().equals("COMBO_STATE6"))
337 combo11.setEnabled(comboState6.isSelected());
338 combo12.setEnabled(comboState6.isSelected());
340 else if (e.getActionCommand().equals("CLOSE"))
342 System.exit(0);
346 public static void main(String[] args)
350 UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
352 catch (Exception e) {
353 e.printStackTrace();
355 ComboBoxDemo app = new ComboBoxDemo("ComboBox Demo");
356 app.pack();
357 app.setVisible(true);