1
35
36
39
40
41 import javax.swing.*;
42 import javax.swing.event.*;
43 import javax.swing.text.*;
44 import javax.swing.border.*;
45 import javax.swing.colorchooser.*;
46 import javax.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import java.awt.*;
50 import java.awt.event.*;
51 import java.beans.*;
52 import java.util.*;
53 import java.io.*;
54 import java.applet.*;
55 import java.net.*;
56
57
63 public class ComboBoxDemo extends DemoModule implements ActionListener {
64
65 Face face;
66 JLabel faceLabel;
67
68 JComboBox hairCB;
69 JComboBox eyesCB;
70 JComboBox mouthCB;
71
72 JComboBox presetCB;
73
74 Hashtable parts = new Hashtable();
75
76
79 public static void main(String[] args) {
80 ComboBoxDemo demo = new ComboBoxDemo(null);
81 demo.mainImpl();
82 }
83
84
87 public ComboBoxDemo(SwingSet2 swingset) {
88 super(swingset, "ComboBoxDemo", "toolbar/JComboBox.gif");
91
92 createComboBoxDemo();
93 }
94
95 public void createComboBoxDemo() {
96 JPanel demo = getDemoPanel();
97
98 JPanel demoPanel = getDemoPanel();
99 demoPanel.setLayout(new BoxLayout(demoPanel, BoxLayout.Y_AXIS));
100
101 JPanel innerPanel = new JPanel();
102 innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));
103
104 demoPanel.add(Box.createRigidArea(VGAP20));
105 demoPanel.add(innerPanel);
106 demoPanel.add(Box.createRigidArea(VGAP20));
107
108 innerPanel.add(Box.createRigidArea(HGAP20));
109
110 JPanel comboBoxPanel = new JPanel() {
112 public Dimension getMaximumSize() {
113 return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
114 }
115 };
116 comboBoxPanel.setLayout(new BoxLayout(comboBoxPanel, BoxLayout.Y_AXIS));
117
118 comboBoxPanel.add(Box.createRigidArea(VGAP15));
119
120 JLabel l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.presets")));
121 l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
122 presetCB = (JComboBox) comboBoxPanel.add(createPresetComboBox());
123 presetCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
124 l.setLabelFor(presetCB);
125 comboBoxPanel.add(Box.createRigidArea(VGAP30));
126
127 l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.hair_description")));
128 l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
129 hairCB = (JComboBox) comboBoxPanel.add(createHairComboBox());
130 hairCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
131 l.setLabelFor(hairCB);
132 comboBoxPanel.add(Box.createRigidArea(VGAP15));
133
134 l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.eyes_description")));
135 l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
136 eyesCB = (JComboBox) comboBoxPanel.add(createEyesComboBox());
137 eyesCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
138 l.setLabelFor(eyesCB);
139 comboBoxPanel.add(Box.createRigidArea(VGAP15));
140
141 l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.mouth_description")));
142 l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
143 mouthCB = (JComboBox) comboBoxPanel.add(createMouthComboBox());
144 mouthCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
145 l.setLabelFor(mouthCB);
146 comboBoxPanel.add(Box.createRigidArea(VGAP15));
147
148 comboBoxPanel.add(new JPanel(new BorderLayout()));
150
151
153 face = new Face();
154 JPanel facePanel = new JPanel();
155 facePanel.setLayout(new BorderLayout());
156 facePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
157
158 faceLabel = new JLabel(face);
159 facePanel.add(faceLabel, BorderLayout.CENTER);
160 Object [] controlledByObjects = new Object[3];
163 controlledByObjects[0] = hairCB;
164 controlledByObjects[1] = eyesCB;
165 controlledByObjects[2] = mouthCB;
166 AccessibleRelation controlledByRelation =
167 new AccessibleRelation(AccessibleRelation.CONTROLLED_BY_PROPERTY,
168 controlledByObjects);
169 facePanel.getAccessibleContext().getAccessibleRelationSet().add(controlledByRelation);
170
171 AccessibleRelation controllerForRelation =
174 new AccessibleRelation(AccessibleRelation.CONTROLLER_FOR_PROPERTY,
175 facePanel);
176 hairCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
177 eyesCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
178 mouthCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
179
180 innerPanel.add(comboBoxPanel);
182 innerPanel.add(Box.createRigidArea(HGAP30));
183 innerPanel.add(facePanel);
184 innerPanel.add(Box.createRigidArea(HGAP20));
185
186 addFace("brent", getString("ComboBoxDemo.brent"));
188 addFace("georges", getString("ComboBoxDemo.georges"));
189 addFace("hans", getString("ComboBoxDemo.hans"));
190 addFace("howard", getString("ComboBoxDemo.howard"));
191 addFace("james", getString("ComboBoxDemo.james"));
192 addFace("jeff", getString("ComboBoxDemo.jeff"));
193 addFace("jon", getString("ComboBoxDemo.jon"));
194 addFace("lara", getString("ComboBoxDemo.lara"));
195 addFace("larry", getString("ComboBoxDemo.larry"));
196 addFace("lisa", getString("ComboBoxDemo.lisa"));
197 addFace("michael", getString("ComboBoxDemo.michael"));
198 addFace("philip", getString("ComboBoxDemo.philip"));
199 addFace("scott", getString("ComboBoxDemo.scott"));
200
201 presetCB.setSelectedIndex(0);
203 }
204
205 void addFace(String name, String i18n_name) {
206 ImageIcon i;
207 String i18n_hair = getString("ComboBoxDemo.hair");
208 String i18n_eyes = getString("ComboBoxDemo.eyes");
209 String i18n_mouth = getString("ComboBoxDemo.mouth");
210
211 parts.put(i18n_name, name); parts.put(name, i18n_name);
214 i = createImageIcon("combobox/" + name + "hair.jpg", i18n_name + i18n_hair);
215 parts.put(name + "hair", i);
216
217 i = createImageIcon("combobox/" + name + "eyes.jpg", i18n_name + i18n_eyes);
218 parts.put(name + "eyes", i);
219
220 i = createImageIcon("combobox/" + name + "mouth.jpg", i18n_name + i18n_mouth);
221 parts.put(name + "mouth", i);
222 }
223
224 Face getFace() {
225 return face;
226 }
227
228 JComboBox createHairComboBox() {
229 JComboBox cb = new JComboBox();
230 fillComboBox(cb);
231 cb.addActionListener(this);
232 return cb;
233 }
234
235 JComboBox createEyesComboBox() {
236 JComboBox cb = new JComboBox();
237 fillComboBox(cb);
238 cb.addActionListener(this);
239 return cb;
240 }
241
242 JComboBox createNoseComboBox() {
243 JComboBox cb = new JComboBox();
244 fillComboBox(cb);
245 cb.addActionListener(this);
246 return cb;
247 }
248
249 JComboBox createMouthComboBox() {
250 JComboBox cb = new JComboBox();
251 fillComboBox(cb);
252 cb.addActionListener(this);
253 return cb;
254 }
255
256 JComboBox createPresetComboBox() {
257 JComboBox cb = new JComboBox();
258 cb.addItem(getString("ComboBoxDemo.preset1"));
259 cb.addItem(getString("ComboBoxDemo.preset2"));
260 cb.addItem(getString("ComboBoxDemo.preset3"));
261 cb.addItem(getString("ComboBoxDemo.preset4"));
262 cb.addItem(getString("ComboBoxDemo.preset5"));
263 cb.addItem(getString("ComboBoxDemo.preset6"));
264 cb.addItem(getString("ComboBoxDemo.preset7"));
265 cb.addItem(getString("ComboBoxDemo.preset8"));
266 cb.addItem(getString("ComboBoxDemo.preset9"));
267 cb.addItem(getString("ComboBoxDemo.preset10"));
268 cb.addActionListener(this);
269 return cb;
270 }
271
272 void fillComboBox(JComboBox cb) {
273 cb.addItem(getString("ComboBoxDemo.brent"));
274 cb.addItem(getString("ComboBoxDemo.georges"));
275 cb.addItem(getString("ComboBoxDemo.hans"));
276 cb.addItem(getString("ComboBoxDemo.howard"));
277 cb.addItem(getString("ComboBoxDemo.james"));
278 cb.addItem(getString("ComboBoxDemo.jeff"));
279 cb.addItem(getString("ComboBoxDemo.jon"));
280 cb.addItem(getString("ComboBoxDemo.lara"));
281 cb.addItem(getString("ComboBoxDemo.larry"));
282 cb.addItem(getString("ComboBoxDemo.lisa"));
283 cb.addItem(getString("ComboBoxDemo.michael"));
284 cb.addItem(getString("ComboBoxDemo.philip"));
285 cb.addItem(getString("ComboBoxDemo.scott"));
286 }
287
288 public void actionPerformed(ActionEvent e) {
289 if(e.getSource() == hairCB) {
290 String name = (String) parts.get((String) hairCB.getSelectedItem());
291 face.setHair((ImageIcon) parts.get(name + "hair"));
292 faceLabel.repaint();
293 } else if(e.getSource() == eyesCB) {
294 String name = (String) parts.get((String) eyesCB.getSelectedItem());
295 face.setEyes((ImageIcon) parts.get(name + "eyes"));
296 faceLabel.repaint();
297 } else if(e.getSource() == mouthCB) {
298 String name = (String) parts.get((String) mouthCB.getSelectedItem());
299 face.setMouth((ImageIcon) parts.get(name + "mouth"));
300 faceLabel.repaint();
301 } else if(e.getSource() == presetCB) {
302 String hair = null;
303 String eyes = null;
304 String mouth = null;
305 switch(presetCB.getSelectedIndex()) {
306 case 0:
307 hair = (String) parts.get("philip");
308 eyes = (String) parts.get("howard");
309 mouth = (String) parts.get("jeff");
310 break;
311 case 1:
312 hair = (String) parts.get("jeff");
313 eyes = (String) parts.get("larry");
314 mouth = (String) parts.get("philip");
315 break;
316 case 2:
317 hair = (String) parts.get("howard");
318 eyes = (String) parts.get("scott");
319 mouth = (String) parts.get("hans");
320 break;
321 case 3:
322 hair = (String) parts.get("philip");
323 eyes = (String) parts.get("jeff");
324 mouth = (String) parts.get("hans");
325 break;
326 case 4:
327 hair = (String) parts.get("brent");
328 eyes = (String) parts.get("jon");
329 mouth = (String) parts.get("scott");
330 break;
331 case 5:
332 hair = (String) parts.get("lara");
333 eyes = (String) parts.get("larry");
334 mouth = (String) parts.get("lisa");
335 break;
336 case 6:
337 hair = (String) parts.get("james");
338 eyes = (String) parts.get("philip");
339 mouth = (String) parts.get("michael");
340 break;
341 case 7:
342 hair = (String) parts.get("philip");
343 eyes = (String) parts.get("lisa");
344 mouth = (String) parts.get("brent");
345 break;
346 case 8:
347 hair = (String) parts.get("james");
348 eyes = (String) parts.get("philip");
349 mouth = (String) parts.get("jon");
350 break;
351 case 9:
352 hair = (String) parts.get("lara");
353 eyes = (String) parts.get("jon");
354 mouth = (String) parts.get("scott");
355 break;
356 }
357 if(hair != null) {
358 hairCB.setSelectedItem(hair);
359 eyesCB.setSelectedItem(eyes);
360 mouthCB.setSelectedItem(mouth);
361 faceLabel.repaint();
362 }
363 }
364 }
365
366 class Face implements Icon {
367 ImageIcon hair;
368 ImageIcon eyes;
369 ImageIcon mouth;
370
371 void setHair(ImageIcon i) {
372 hair = i;
373 }
374
375 void setEyes(ImageIcon i) {
376 eyes = i;
377 }
378
379 void setMouth(ImageIcon i) {
380 mouth = i;
381 }
382
383 public void paintIcon(Component c, Graphics g, int x, int y) {
384 int height = y;
385 x = c.getWidth()/2 - getIconWidth()/2;
386
387 if(hair != null) {
388 hair.paintIcon(c, g, x, height); height += hair.getIconHeight();
389 }
390
391 if(eyes != null) {
392 eyes.paintIcon(c, g, x, height); height += eyes.getIconHeight();
393 }
394
395 if(mouth != null) {
396 mouth.paintIcon(c, g, x, height);
397 }
398 }
399
400 public int getIconWidth() {
401 return 344;
402 }
403
404 public int getIconHeight() {
405 return 455;
406 }
407 }
408 }
409
410