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