KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > ComboBoxDemo


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

35
36 /*
37  * @(#)ComboBoxDemo.java 1.10 03/01/23
38  */

39 package demo.swingset;
40
41 import swingwtx.swing.*;
42 import swingwtx.swing.event.*;
43 import swingwtx.swing.text.*;
44 import swingwtx.swing.border.*;
45 import swingwtx.swing.colorchooser.*;
46 import swingwtx.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import swingwt.awt.*;
50 import swingwt.awt.event.*;
51 import swingwt.awt.image.*;
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.10 01/23/03
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();
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         getDemoPanel().invalidate();
206     }
207     
208     void addFace(String JavaDoc name, String JavaDoc i18n_name) {
209         ImageIcon i;
210         String JavaDoc i18n_hair = getString("ComboBoxDemo.hair");
211         String JavaDoc i18n_eyes = getString("ComboBoxDemo.eyes");
212         String JavaDoc i18n_mouth = getString("ComboBoxDemo.mouth");
213         
214         parts.put(i18n_name, name); // i18n name lookup
215
parts.put(name, i18n_name); // reverse name lookup
216

217         i = createImageIcon("combobox/" + name + "hair.jpg", i18n_name + i18n_hair);
218         parts.put(name + "hair", i);
219         
220         i = createImageIcon("combobox/" + name + "eyes.jpg", i18n_name + i18n_eyes);
221         parts.put(name + "eyes", i);
222         
223         i = createImageIcon("combobox/" + name + "mouth.jpg", i18n_name + i18n_mouth);
224         parts.put(name + "mouth", i);
225     }
226     
227     Face getFace() {
228         return face;
229     }
230     
231     JComboBox createHairComboBox() {
232         JComboBox cb = new JComboBox();
233         fillComboBox(cb);
234         cb.addActionListener(this);
235         return cb;
236     }
237     
238     JComboBox createEyesComboBox() {
239         JComboBox cb = new JComboBox();
240         fillComboBox(cb);
241         cb.addActionListener(this);
242         return cb;
243     }
244     
245     JComboBox createNoseComboBox() {
246         JComboBox cb = new JComboBox();
247         fillComboBox(cb);
248         cb.addActionListener(this);
249         return cb;
250     }
251     
252     JComboBox createMouthComboBox() {
253         JComboBox cb = new JComboBox();
254         fillComboBox(cb);
255         cb.addActionListener(this);
256         return cb;
257     }
258     
259     JComboBox createPresetComboBox() {
260         JComboBox cb = new JComboBox();
261         cb.addItem(getString("ComboBoxDemo.preset1"));
262         cb.addItem(getString("ComboBoxDemo.preset2"));
263         cb.addItem(getString("ComboBoxDemo.preset3"));
264         cb.addItem(getString("ComboBoxDemo.preset4"));
265         cb.addItem(getString("ComboBoxDemo.preset5"));
266         cb.addItem(getString("ComboBoxDemo.preset6"));
267         cb.addItem(getString("ComboBoxDemo.preset7"));
268         cb.addItem(getString("ComboBoxDemo.preset8"));
269         cb.addItem(getString("ComboBoxDemo.preset9"));
270         cb.addItem(getString("ComboBoxDemo.preset10"));
271         cb.addActionListener(this);
272         return cb;
273     }
274     
275     void fillComboBox(JComboBox cb) {
276         cb.addItem(getString("ComboBoxDemo.brent"));
277         cb.addItem(getString("ComboBoxDemo.georges"));
278         cb.addItem(getString("ComboBoxDemo.hans"));
279         cb.addItem(getString("ComboBoxDemo.howard"));
280         cb.addItem(getString("ComboBoxDemo.james"));
281         cb.addItem(getString("ComboBoxDemo.jeff"));
282         cb.addItem(getString("ComboBoxDemo.jon"));
283         cb.addItem(getString("ComboBoxDemo.lara"));
284         cb.addItem(getString("ComboBoxDemo.larry"));
285         cb.addItem(getString("ComboBoxDemo.lisa"));
286         cb.addItem(getString("ComboBoxDemo.michael"));
287         cb.addItem(getString("ComboBoxDemo.philip"));
288         cb.addItem(getString("ComboBoxDemo.scott"));
289     }
290     
291     public void actionPerformed(ActionEvent e) {
292         if(e.getSource() == hairCB) {
293             String JavaDoc name = (String JavaDoc) parts.get((String JavaDoc) hairCB.getSelectedItem());
294             face.setHair((ImageIcon) parts.get(name + "hair"));
295             if (face.hasAllBits()) faceLabel.setIcon(face);
296         } else if(e.getSource() == eyesCB) {
297             String JavaDoc name = (String JavaDoc) parts.get((String JavaDoc) eyesCB.getSelectedItem());
298             face.setEyes((ImageIcon) parts.get(name + "eyes"));
299             if (face.hasAllBits()) faceLabel.setIcon(face);
300         } else if(e.getSource() == mouthCB) {
301             String JavaDoc name = (String JavaDoc) parts.get((String JavaDoc) mouthCB.getSelectedItem());
302             face.setMouth((ImageIcon) parts.get(name + "mouth"));
303             if (face.hasAllBits()) faceLabel.setIcon(face);
304         } else if(e.getSource() == presetCB) {
305             String JavaDoc hair = null;
306             String JavaDoc eyes = null;
307             String JavaDoc mouth = null;
308             switch(presetCB.getSelectedIndex()) {
309                 case 0:
310                     hair = (String JavaDoc) parts.get("philip");
311                     eyes = (String JavaDoc) parts.get("howard");
312                     mouth = (String JavaDoc) parts.get("jeff");
313                     break;
314                 case 1:
315                     hair = (String JavaDoc) parts.get("jeff");
316                     eyes = (String JavaDoc) parts.get("larry");
317                     mouth = (String JavaDoc) parts.get("philip");
318                     break;
319                 case 2:
320                     hair = (String JavaDoc) parts.get("howard");
321                     eyes = (String JavaDoc) parts.get("scott");
322                     mouth = (String JavaDoc) parts.get("hans");
323                     break;
324                 case 3:
325                     hair = (String JavaDoc) parts.get("philip");
326                     eyes = (String JavaDoc) parts.get("jeff");
327                     mouth = (String JavaDoc) parts.get("hans");
328                     break;
329                 case 4:
330                     hair = (String JavaDoc) parts.get("brent");
331                     eyes = (String JavaDoc) parts.get("jon");
332                     mouth = (String JavaDoc) parts.get("scott");
333                     break;
334                 case 5:
335                     hair = (String JavaDoc) parts.get("lara");
336                     eyes = (String JavaDoc) parts.get("larry");
337                     mouth = (String JavaDoc) parts.get("lisa");
338                     break;
339                 case 6:
340                     hair = (String JavaDoc) parts.get("james");
341                     eyes = (String JavaDoc) parts.get("philip");
342                     mouth = (String JavaDoc) parts.get("michael");
343                     break;
344                 case 7:
345                     hair = (String JavaDoc) parts.get("philip");
346                     eyes = (String JavaDoc) parts.get("lisa");
347                     mouth = (String JavaDoc) parts.get("brent");
348                     break;
349                 case 8:
350                     hair = (String JavaDoc) parts.get("james");
351                     eyes = (String JavaDoc) parts.get("philip");
352                     mouth = (String JavaDoc) parts.get("jon");
353                     break;
354                 case 9:
355                     hair = (String JavaDoc) parts.get("lara");
356                     eyes = (String JavaDoc) parts.get("jon");
357                     mouth = (String JavaDoc) parts.get("scott");
358                     break;
359             }
360             if(hair != null) {
361                 hairCB.setSelectedItem(hair);
362                 eyesCB.setSelectedItem(eyes);
363                 mouthCB.setSelectedItem(mouth);
364                 if (face.hasAllBits()) faceLabel.setIcon(face);
365             }
366         }
367         
368         getDemoPanel().invalidate();
369     }
370     
371     class Face extends ImageIcon {
372         ImageIcon hair;
373         ImageIcon eyes;
374         ImageIcon mouth;
375         
376         void setHair(ImageIcon i) {
377             hair = i;
378         }
379         
380         void setEyes(ImageIcon i) {
381             eyes = i;
382         }
383         
384         void setMouth(ImageIcon i) {
385             mouth = i;
386         }
387         
388         public boolean hasAllBits() {
389             return hair != null && eyes != null && mouth != null;
390         }
391         
392         public void paintIcon(Component c, Graphics g, int x, int y) {
393             int height = y;
394             x = c.getWidth()/2 - getIconWidth()/2;
395             
396             if(hair != null) {
397                 hair.paintIcon(c, g, x, height); height += hair.getIconHeight();
398             }
399             
400             if(eyes != null) {
401                 eyes.paintIcon(c, g, x, height); height += eyes.getIconHeight();
402             }
403             
404             if(mouth != null) {
405                 mouth.paintIcon(c, g, x, height);
406             }
407         }
408         
409         public Image getImage() {
410             if (hair == null || eyes == null || mouth == null) return null;
411             BufferedImage b = new BufferedImage(getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_RGB);
412             Graphics g = b.getGraphics();
413             g.drawImage( hair.getImage(), 0, 0, null );
414             g.drawImage( eyes.getImage(), 0, hair.getIconHeight(), null );
415             g.drawImage( mouth.getImage(), 0, hair.getIconHeight() + eyes.getIconHeight(), null);
416             g.dispose();
417             return b;
418         }
419         
420         public int getIconWidth() {
421             return 344;
422         }
423         
424         public int getIconHeight() {
425             return 455;
426         }
427     }
428 }
429
430
Popular Tags