KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ComboBoxDemo


1 /*
2  * @(#)ComboBoxDemo.java 1.13 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)ComboBoxDemo.java 1.13 05/11/17
39  */

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 /**
59  * JComboBox Demo
60  *
61  * @version 1.13 11/17/05
62  * @author Jeff Dinkins
63  */

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     /**
78      * main method allows us to run as a standalone demo.
79      */

80     public static void main(String JavaDoc[] args) {
81     ComboBoxDemo demo = new ComboBoxDemo(null);
82     demo.mainImpl();
83     }
84   
85     /**
86      * ComboBoxDemo Constructor
87      */

88     public ComboBoxDemo(SwingSet2 swingset) {
89     // Set the title for this demo, and an icon used to represent this
90
// demo inside the SwingSet2 app.
91
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     // Create a panel to hold buttons
112
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         // Fill up the remaining space
150
comboBoxPanel.add(new JPanel(new BorderLayout()));
151
152     // Create and place the Face.
153

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     // Indicate that the face panel is controlled by the hair, eyes and
162
// mouth combo boxes.
163
Object JavaDoc [] controlledByObjects = new Object JavaDoc[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     // Indicate that the hair, eyes and mouth combo boxes are controllers
173
// for the face panel.
174
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     // add buttons and image panels to inner panel
182
innerPanel.add(comboBoxPanel);
183     innerPanel.add(Box.createRigidArea(HGAP30));
184     innerPanel.add(facePanel);
185     innerPanel.add(Box.createRigidArea(HGAP20));
186
187     // load up the face parts
188
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     // set the default face
203
presetCB.setSelectedIndex(0);
204     }
205
206     void addFace(String JavaDoc name, String JavaDoc i18n_name) {
207     ImageIcon i;
208     String JavaDoc i18n_hair = getString("ComboBoxDemo.hair");
209     String JavaDoc i18n_eyes = getString("ComboBoxDemo.eyes");
210     String JavaDoc i18n_mouth = getString("ComboBoxDemo.mouth");
211     
212     parts.put(i18n_name, name); // i18n name lookup
213
parts.put(name, i18n_name); // reverse name lookup
214

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 JavaDoc name = (String JavaDoc) parts.get((String JavaDoc) hairCB.getSelectedItem());
292         face.setHair((ImageIcon) parts.get(name + "hair"));
293         faceLabel.repaint();
294     } else if(e.getSource() == eyesCB) {
295         String JavaDoc name = (String JavaDoc) parts.get((String JavaDoc) eyesCB.getSelectedItem());
296         face.setEyes((ImageIcon) parts.get(name + "eyes"));
297         faceLabel.repaint();
298     } else if(e.getSource() == mouthCB) {
299         String JavaDoc name = (String JavaDoc) parts.get((String JavaDoc) mouthCB.getSelectedItem());
300         face.setMouth((ImageIcon) parts.get(name + "mouth"));
301         faceLabel.repaint();
302     } else if(e.getSource() == presetCB) {
303         String JavaDoc hair = null;
304         String JavaDoc eyes = null;
305         String JavaDoc mouth = null;
306         switch(presetCB.getSelectedIndex()) {
307            case 0:
308            hair = (String JavaDoc) parts.get("philip");
309            eyes = (String JavaDoc) parts.get("howard");
310            mouth = (String JavaDoc) parts.get("jeff");
311            break;
312            case 1:
313            hair = (String JavaDoc) parts.get("jeff");
314            eyes = (String JavaDoc) parts.get("larry");
315            mouth = (String JavaDoc) parts.get("philip");
316            break;
317            case 2:
318            hair = (String JavaDoc) parts.get("howard");
319            eyes = (String JavaDoc) parts.get("scott");
320            mouth = (String JavaDoc) parts.get("hans");
321            break;
322            case 3:
323            hair = (String JavaDoc) parts.get("philip");
324            eyes = (String JavaDoc) parts.get("jeff");
325            mouth = (String JavaDoc) parts.get("hans");
326            break;
327            case 4:
328            hair = (String JavaDoc) parts.get("brent");
329            eyes = (String JavaDoc) parts.get("jon");
330            mouth = (String JavaDoc) parts.get("scott");
331            break;
332            case 5:
333            hair = (String JavaDoc) parts.get("lara");
334            eyes = (String JavaDoc) parts.get("larry");
335            mouth = (String JavaDoc) parts.get("lisa");
336            break;
337            case 6:
338            hair = (String JavaDoc) parts.get("james");
339            eyes = (String JavaDoc) parts.get("philip");
340            mouth = (String JavaDoc) parts.get("michael");
341            break;
342            case 7:
343            hair = (String JavaDoc) parts.get("philip");
344            eyes = (String JavaDoc) parts.get("lisa");
345            mouth = (String JavaDoc) parts.get("brent");
346            break;
347            case 8:
348            hair = (String JavaDoc) parts.get("james");
349            eyes = (String JavaDoc) parts.get("philip");
350            mouth = (String JavaDoc) parts.get("jon");
351            break;
352            case 9:
353            hair = (String JavaDoc) parts.get("lara");
354            eyes = (String JavaDoc) parts.get("jon");
355            mouth = (String JavaDoc) 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
Popular Tags