1 /* ComboBoxDemo.java -- An example showing various combo boxes in Swing.
2 Copyright (C) 2005, 2006, 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)
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
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
;
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
.JComponent
;
41 import javax
.swing
.JFrame
;
42 import javax
.swing
.JList
;
43 import javax
.swing
.JPanel
;
44 import javax
.swing
.SwingUtilities
;
45 import javax
.swing
.plaf
.metal
.MetalIconFactory
;
48 * A simple demo showing various combo boxes in different states.
50 public class ComboBoxDemo
52 implements ActionListener
55 class CustomCellRenderer
extends DefaultListCellRenderer
57 public Component
getListCellRendererComponent(JList list
,
63 DefaultListCellRenderer result
= (DefaultListCellRenderer
)
64 super.getListCellRendererComponent(list
, value
, index
, isSelected
,
66 Icon icon
= (Icon
) value
;
68 result
.setText("Index = " + index
);
73 private JCheckBox comboState1
;
74 private JComboBox combo1
;
75 private JComboBox combo2
;
77 private JCheckBox comboState2
;
78 private JComboBox combo3
;
79 private JComboBox combo4
;
81 private JCheckBox comboState3
;
82 private JComboBox combo5
;
83 private JComboBox combo6
;
85 private JCheckBox comboState4
;
86 private JComboBox combo7
;
87 private JComboBox combo8
;
89 private JCheckBox comboState5
;
90 private JComboBox combo9
;
91 private JComboBox combo10
;
93 private JCheckBox comboState6
;
94 private JComboBox combo11
;
95 private JComboBox combo12
;
98 * Creates a new demo instance.
100 public ComboBoxDemo()
107 * When the demo is run independently, the frame is displayed, so we should
108 * initialise the content panel (including the demo content and a close
109 * button). But when the demo is run as part of the Swing activity board,
110 * only the demo content panel is used, the frame itself is never displayed,
111 * so we can avoid this step.
113 void initFrameContent()
115 JPanel closePanel
= new JPanel();
116 JButton closeButton
= new JButton("Close");
117 closeButton
.setActionCommand("CLOSE");
118 closeButton
.addActionListener(this);
119 closePanel
.add(closeButton
);
120 add(closePanel
, BorderLayout
.SOUTH
);
124 * Returns a panel with the demo content. The panel
125 * uses a BorderLayout(), and the BorderLayout.SOUTH area
126 * is empty, to allow callers to add controls to the
127 * bottom of the panel if they want to (a close button is
128 * added if this demo is being run as a standalone demo).
130 private void createContent()
132 setLayout(new BorderLayout());
133 JPanel panel
= new JPanel(new GridLayout(6, 1));
134 panel
.add(createPanel1());
135 panel
.add(createPanel2());
136 panel
.add(createPanel3());
137 panel
.add(createPanel4());
138 panel
.add(createPanel5());
139 panel
.add(createPanel6());
143 private JPanel
createPanel1()
145 JPanel panel
= new JPanel(new BorderLayout());
146 this.comboState1
= new JCheckBox("Enabled", true);
147 this.comboState1
.setActionCommand("COMBO_STATE1");
148 this.comboState1
.addActionListener(this);
149 panel
.add(this.comboState1
, BorderLayout
.EAST
);
151 JPanel controlPanel
= new JPanel();
152 controlPanel
.setBorder(BorderFactory
.createTitledBorder("Regular: "));
153 this.combo1
= new JComboBox(new Object
[] {"Australia", "New Zealand",
156 this.combo2
= new JComboBox(new Object
[] {"Australia", "New Zealand",
158 this.combo2
.setEditable(true);
160 controlPanel
.add(combo1
);
161 controlPanel
.add(combo2
);
163 panel
.add(controlPanel
);
168 private JPanel
createPanel2()
170 JPanel panel
= new JPanel(new BorderLayout());
171 this.comboState2
= new JCheckBox("Enabled", true);
172 this.comboState2
.setActionCommand("COMBO_STATE2");
173 this.comboState2
.addActionListener(this);
174 panel
.add(this.comboState2
, BorderLayout
.EAST
);
176 JPanel controlPanel
= new JPanel();
177 controlPanel
.setBorder(BorderFactory
.createTitledBorder("Large Font: "));
178 this.combo3
= new JComboBox(new Object
[] {"Australia", "New Zealand",
180 this.combo3
.setFont(new Font("Dialog", Font
.PLAIN
, 20));
182 this.combo4
= new JComboBox(new Object
[] {"Australia", "New Zealand",
184 this.combo4
.setEditable(true);
185 this.combo4
.setFont(new Font("Dialog", Font
.PLAIN
, 20));
187 controlPanel
.add(combo3
);
188 controlPanel
.add(combo4
);
190 panel
.add(controlPanel
);
195 private JPanel
createPanel3()
197 JPanel panel
= new JPanel(new BorderLayout());
198 this.comboState3
= new JCheckBox("Enabled", true);
199 this.comboState3
.setActionCommand("COMBO_STATE3");
200 this.comboState3
.addActionListener(this);
201 panel
.add(this.comboState3
, BorderLayout
.EAST
);
203 JPanel controlPanel
= new JPanel();
204 controlPanel
.setBorder(BorderFactory
.createTitledBorder("Colored Background: "));
205 this.combo5
= new JComboBox(new Object
[] {"Australia", "New Zealand",
207 this.combo5
.setBackground(Color
.yellow
);
209 this.combo6
= new JComboBox(new Object
[] {"Australia", "New Zealand",
211 this.combo6
.setEditable(true);
212 this.combo6
.setBackground(Color
.yellow
);
214 controlPanel
.add(combo5
);
215 controlPanel
.add(combo6
);
217 panel
.add(controlPanel
);
223 * This panel contains combo boxes that are empty.
227 private JPanel
createPanel4()
229 JPanel panel
= new JPanel(new BorderLayout());
230 this.comboState4
= new JCheckBox("Enabled", true);
231 this.comboState4
.setActionCommand("COMBO_STATE4");
232 this.comboState4
.addActionListener(this);
233 panel
.add(this.comboState4
, BorderLayout
.EAST
);
235 JPanel controlPanel
= new JPanel();
236 controlPanel
.setBorder(BorderFactory
.createTitledBorder("Empty: "));
237 this.combo7
= new JComboBox();
238 this.combo8
= new JComboBox();
239 this.combo8
.setEditable(true);
241 controlPanel
.add(combo7
);
242 controlPanel
.add(combo8
);
244 panel
.add(controlPanel
);
250 * This panel contains combo boxes that are narrow but contain long text
255 private JPanel
createPanel5()
257 JPanel panel
= new JPanel(new BorderLayout());
258 this.comboState5
= new JCheckBox("Enabled", true);
259 this.comboState5
.setActionCommand("COMBO_STATE5");
260 this.comboState5
.addActionListener(this);
261 panel
.add(this.comboState5
, BorderLayout
.EAST
);
263 JPanel controlPanel
= new JPanel();
264 controlPanel
.setBorder(BorderFactory
.createTitledBorder("Narrow: "));
265 this.combo9
= new JComboBox(new Object
[] {
266 "A really long item that will be truncated when displayed"});
267 this.combo9
.setPreferredSize(new Dimension(100, 30));
268 this.combo10
= new JComboBox(new Object
[] {
269 "A really long item that will be truncated when displayed"});
270 this.combo10
.setPreferredSize(new Dimension(100, 30));
271 this.combo10
.setEditable(true);
273 controlPanel
.add(combo9
);
274 controlPanel
.add(combo10
);
276 panel
.add(controlPanel
);
282 * This panel contains combo boxes with a custom renderer.
286 private JPanel
createPanel6()
288 JPanel panel
= new JPanel(new BorderLayout());
289 this.comboState6
= new JCheckBox("Enabled", true);
290 this.comboState6
.setActionCommand("COMBO_STATE6");
291 this.comboState6
.addActionListener(this);
292 panel
.add(this.comboState6
, BorderLayout
.EAST
);
294 JPanel controlPanel
= new JPanel();
295 controlPanel
.setBorder(BorderFactory
.createTitledBorder("Custom Renderer: "));
296 this.combo11
= new JComboBox(new Object
[] {
297 MetalIconFactory
.getFileChooserHomeFolderIcon(),
298 MetalIconFactory
.getFileChooserNewFolderIcon()});
299 this.combo11
.setPreferredSize(new Dimension(100, 30));
300 this.combo11
.setRenderer(new CustomCellRenderer());
301 this.combo12
= new JComboBox(new Object
[] {
302 MetalIconFactory
.getFileChooserHomeFolderIcon(),
303 MetalIconFactory
.getFileChooserNewFolderIcon()});
304 this.combo12
.setPreferredSize(new Dimension(100, 30));
305 this.combo12
.setRenderer(new CustomCellRenderer());
306 this.combo12
.setEditable(true);
308 controlPanel
.add(combo11
);
309 controlPanel
.add(combo12
);
311 panel
.add(controlPanel
);
316 public void actionPerformed(ActionEvent e
)
318 if (e
.getActionCommand().equals("COMBO_STATE1"))
320 combo1
.setEnabled(comboState1
.isSelected());
321 combo2
.setEnabled(comboState1
.isSelected());
323 else if (e
.getActionCommand().equals("COMBO_STATE2"))
325 combo3
.setEnabled(comboState2
.isSelected());
326 combo4
.setEnabled(comboState2
.isSelected());
328 else if (e
.getActionCommand().equals("COMBO_STATE3"))
330 combo5
.setEnabled(comboState3
.isSelected());
331 combo6
.setEnabled(comboState3
.isSelected());
333 else if (e
.getActionCommand().equals("COMBO_STATE4"))
335 combo7
.setEnabled(comboState4
.isSelected());
336 combo8
.setEnabled(comboState4
.isSelected());
338 else if (e
.getActionCommand().equals("COMBO_STATE5"))
340 combo9
.setEnabled(comboState5
.isSelected());
341 combo10
.setEnabled(comboState5
.isSelected());
343 else if (e
.getActionCommand().equals("COMBO_STATE6"))
345 combo11
.setEnabled(comboState6
.isSelected());
346 combo12
.setEnabled(comboState6
.isSelected());
348 else if (e
.getActionCommand().equals("CLOSE"))
354 public static void main(String
[] args
)
356 SwingUtilities
.invokeLater
361 ComboBoxDemo app
= new ComboBoxDemo();
362 app
.initFrameContent();
363 JFrame frame
= new JFrame();
364 frame
.getContentPane().add(app
);
366 frame
.setVisible(true);
372 * Returns a DemoFactory that creates a ComboBoxDemo.
374 * @return a DemoFactory that creates a ComboBoxDemo
376 public static DemoFactory
createDemoFactory()
378 return new DemoFactory()
380 public JComponent
createDemo()
382 return new ComboBoxDemo();