49495
|
1 |
/*
|
|
2 |
*
|
|
3 |
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
|
4 |
*
|
|
5 |
* Redistribution and use in source and binary forms, with or without
|
|
6 |
* modification, are permitted provided that the following conditions
|
|
7 |
* are met:
|
|
8 |
*
|
|
9 |
* - Redistributions of source code must retain the above copyright
|
|
10 |
* notice, this list of conditions and the following disclaimer.
|
|
11 |
*
|
|
12 |
* - Redistributions in binary form must reproduce the above copyright
|
|
13 |
* notice, this list of conditions and the following disclaimer in the
|
|
14 |
* documentation and/or other materials provided with the distribution.
|
|
15 |
*
|
|
16 |
* - Neither the name of Oracle nor the names of its
|
|
17 |
* contributors may be used to endorse or promote products derived
|
|
18 |
* from this software without specific prior written permission.
|
|
19 |
*
|
|
20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
21 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
22 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
23 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
24 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
25 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
26 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
27 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
28 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
29 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
30 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
31 |
*/
|
|
32 |
|
|
33 |
|
|
34 |
import javax.swing.*;
|
|
35 |
import javax.swing.event.*;
|
|
36 |
import javax.swing.text.*;
|
|
37 |
import javax.swing.border.*;
|
|
38 |
import javax.swing.colorchooser.*;
|
|
39 |
import javax.swing.filechooser.*;
|
|
40 |
import javax.accessibility.*;
|
|
41 |
|
|
42 |
import java.awt.*;
|
|
43 |
import java.awt.event.*;
|
|
44 |
import java.beans.*;
|
|
45 |
import java.util.*;
|
|
46 |
import java.io.*;
|
|
47 |
import java.applet.*;
|
|
48 |
import java.net.*;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* JComboBox Demo
|
|
52 |
*
|
|
53 |
* @author Jeff Dinkins
|
|
54 |
*/
|
|
55 |
public class ComboBoxDemo extends DemoModule implements ActionListener {
|
|
56 |
|
|
57 |
Face face;
|
|
58 |
JLabel faceLabel;
|
|
59 |
|
|
60 |
JComboBox hairCB;
|
|
61 |
JComboBox eyesCB;
|
|
62 |
JComboBox mouthCB;
|
|
63 |
|
|
64 |
JComboBox presetCB;
|
|
65 |
|
|
66 |
Hashtable parts = new Hashtable();
|
|
67 |
|
|
68 |
/**
|
|
69 |
* main method allows us to run as a standalone demo.
|
|
70 |
*/
|
|
71 |
public static void main(String[] args) {
|
|
72 |
ComboBoxDemo demo = new ComboBoxDemo(null);
|
|
73 |
demo.mainImpl();
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* ComboBoxDemo Constructor
|
|
78 |
*/
|
|
79 |
public ComboBoxDemo(SwingSet2 swingset) {
|
|
80 |
// Set the title for this demo, and an icon used to represent this
|
|
81 |
// demo inside the SwingSet2 app.
|
|
82 |
super(swingset, "ComboBoxDemo", "toolbar/JComboBox.gif");
|
|
83 |
|
|
84 |
createComboBoxDemo();
|
|
85 |
}
|
|
86 |
|
|
87 |
public void createComboBoxDemo() {
|
|
88 |
JPanel demo = getDemoPanel();
|
|
89 |
|
|
90 |
JPanel demoPanel = getDemoPanel();
|
|
91 |
demoPanel.setLayout(new BoxLayout(demoPanel, BoxLayout.Y_AXIS));
|
|
92 |
|
|
93 |
JPanel innerPanel = new JPanel();
|
|
94 |
innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));
|
|
95 |
|
|
96 |
demoPanel.add(Box.createRigidArea(VGAP20));
|
|
97 |
demoPanel.add(innerPanel);
|
|
98 |
demoPanel.add(Box.createRigidArea(VGAP20));
|
|
99 |
|
|
100 |
innerPanel.add(Box.createRigidArea(HGAP20));
|
|
101 |
|
|
102 |
// Create a panel to hold buttons
|
|
103 |
JPanel comboBoxPanel = new JPanel() {
|
|
104 |
public Dimension getMaximumSize() {
|
|
105 |
return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
|
|
106 |
}
|
|
107 |
};
|
|
108 |
comboBoxPanel.setLayout(new BoxLayout(comboBoxPanel, BoxLayout.Y_AXIS));
|
|
109 |
|
|
110 |
comboBoxPanel.add(Box.createRigidArea(VGAP15));
|
|
111 |
|
|
112 |
JLabel l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.presets")));
|
|
113 |
l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
|
|
114 |
presetCB = (JComboBox) comboBoxPanel.add(createPresetComboBox());
|
|
115 |
presetCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
|
|
116 |
l.setLabelFor(presetCB);
|
|
117 |
comboBoxPanel.add(Box.createRigidArea(VGAP30));
|
|
118 |
|
|
119 |
l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.hair_description")));
|
|
120 |
l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
|
|
121 |
hairCB = (JComboBox) comboBoxPanel.add(createHairComboBox());
|
|
122 |
hairCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
|
|
123 |
l.setLabelFor(hairCB);
|
|
124 |
comboBoxPanel.add(Box.createRigidArea(VGAP15));
|
|
125 |
|
|
126 |
l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.eyes_description")));
|
|
127 |
l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
|
|
128 |
eyesCB = (JComboBox) comboBoxPanel.add(createEyesComboBox());
|
|
129 |
eyesCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
|
|
130 |
l.setLabelFor(eyesCB);
|
|
131 |
comboBoxPanel.add(Box.createRigidArea(VGAP15));
|
|
132 |
|
|
133 |
l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.mouth_description")));
|
|
134 |
l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
|
|
135 |
mouthCB = (JComboBox) comboBoxPanel.add(createMouthComboBox());
|
|
136 |
mouthCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
|
|
137 |
l.setLabelFor(mouthCB);
|
|
138 |
comboBoxPanel.add(Box.createRigidArea(VGAP15));
|
|
139 |
|
|
140 |
// Fill up the remaining space
|
|
141 |
comboBoxPanel.add(new JPanel(new BorderLayout()));
|
|
142 |
|
|
143 |
// Create and place the Face.
|
|
144 |
|
|
145 |
face = new Face();
|
|
146 |
JPanel facePanel = new JPanel();
|
|
147 |
facePanel.setLayout(new BorderLayout());
|
|
148 |
facePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
|
|
149 |
|
|
150 |
faceLabel = new JLabel(face);
|
|
151 |
facePanel.add(faceLabel, BorderLayout.CENTER);
|
|
152 |
// Indicate that the face panel is controlled by the hair, eyes and
|
|
153 |
// mouth combo boxes.
|
|
154 |
Object [] controlledByObjects = new Object[3];
|
|
155 |
controlledByObjects[0] = hairCB;
|
|
156 |
controlledByObjects[1] = eyesCB;
|
|
157 |
controlledByObjects[2] = mouthCB;
|
|
158 |
AccessibleRelation controlledByRelation =
|
|
159 |
new AccessibleRelation(AccessibleRelation.CONTROLLED_BY_PROPERTY,
|
|
160 |
controlledByObjects);
|
|
161 |
facePanel.getAccessibleContext().getAccessibleRelationSet().add(controlledByRelation);
|
|
162 |
|
|
163 |
// Indicate that the hair, eyes and mouth combo boxes are controllers
|
|
164 |
// for the face panel.
|
|
165 |
AccessibleRelation controllerForRelation =
|
|
166 |
new AccessibleRelation(AccessibleRelation.CONTROLLER_FOR_PROPERTY,
|
|
167 |
facePanel);
|
|
168 |
hairCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
|
|
169 |
eyesCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
|
|
170 |
mouthCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
|
|
171 |
|
|
172 |
// add buttons and image panels to inner panel
|
|
173 |
innerPanel.add(comboBoxPanel);
|
|
174 |
innerPanel.add(Box.createRigidArea(HGAP30));
|
|
175 |
innerPanel.add(facePanel);
|
|
176 |
innerPanel.add(Box.createRigidArea(HGAP20));
|
|
177 |
|
|
178 |
// load up the face parts
|
|
179 |
addFace("brent", getString("ComboBoxDemo.brent"));
|
|
180 |
addFace("georges", getString("ComboBoxDemo.georges"));
|
|
181 |
addFace("hans", getString("ComboBoxDemo.hans"));
|
|
182 |
addFace("howard", getString("ComboBoxDemo.howard"));
|
|
183 |
addFace("james", getString("ComboBoxDemo.james"));
|
|
184 |
addFace("jeff", getString("ComboBoxDemo.jeff"));
|
|
185 |
addFace("jon", getString("ComboBoxDemo.jon"));
|
|
186 |
addFace("lara", getString("ComboBoxDemo.lara"));
|
|
187 |
addFace("larry", getString("ComboBoxDemo.larry"));
|
|
188 |
addFace("lisa", getString("ComboBoxDemo.lisa"));
|
|
189 |
addFace("michael", getString("ComboBoxDemo.michael"));
|
|
190 |
addFace("philip", getString("ComboBoxDemo.philip"));
|
|
191 |
addFace("scott", getString("ComboBoxDemo.scott"));
|
|
192 |
|
|
193 |
// set the default face
|
|
194 |
presetCB.setSelectedIndex(0);
|
|
195 |
}
|
|
196 |
|
|
197 |
void addFace(String name, String i18n_name) {
|
|
198 |
ImageIcon i;
|
|
199 |
String i18n_hair = getString("ComboBoxDemo.hair");
|
|
200 |
String i18n_eyes = getString("ComboBoxDemo.eyes");
|
|
201 |
String i18n_mouth = getString("ComboBoxDemo.mouth");
|
|
202 |
|
|
203 |
parts.put(i18n_name, name); // i18n name lookup
|
|
204 |
parts.put(name, i18n_name); // reverse name lookup
|
|
205 |
|
|
206 |
i = createImageIcon("combobox/" + name + "hair.jpg", i18n_name + i18n_hair);
|
|
207 |
parts.put(name + "hair", i);
|
|
208 |
|
|
209 |
i = createImageIcon("combobox/" + name + "eyes.jpg", i18n_name + i18n_eyes);
|
|
210 |
parts.put(name + "eyes", i);
|
|
211 |
|
|
212 |
i = createImageIcon("combobox/" + name + "mouth.jpg", i18n_name + i18n_mouth);
|
|
213 |
parts.put(name + "mouth", i);
|
|
214 |
}
|
|
215 |
|
|
216 |
Face getFace() {
|
|
217 |
return face;
|
|
218 |
}
|
|
219 |
|
|
220 |
JComboBox createHairComboBox() {
|
|
221 |
JComboBox cb = new JComboBox();
|
|
222 |
fillComboBox(cb);
|
|
223 |
cb.addActionListener(this);
|
|
224 |
return cb;
|
|
225 |
}
|
|
226 |
|
|
227 |
JComboBox createEyesComboBox() {
|
|
228 |
JComboBox cb = new JComboBox();
|
|
229 |
fillComboBox(cb);
|
|
230 |
cb.addActionListener(this);
|
|
231 |
return cb;
|
|
232 |
}
|
|
233 |
|
|
234 |
JComboBox createNoseComboBox() {
|
|
235 |
JComboBox cb = new JComboBox();
|
|
236 |
fillComboBox(cb);
|
|
237 |
cb.addActionListener(this);
|
|
238 |
return cb;
|
|
239 |
}
|
|
240 |
|
|
241 |
JComboBox createMouthComboBox() {
|
|
242 |
JComboBox cb = new JComboBox();
|
|
243 |
fillComboBox(cb);
|
|
244 |
cb.addActionListener(this);
|
|
245 |
return cb;
|
|
246 |
}
|
|
247 |
|
|
248 |
JComboBox createPresetComboBox() {
|
|
249 |
JComboBox cb = new JComboBox();
|
|
250 |
cb.addItem(getString("ComboBoxDemo.preset1"));
|
|
251 |
cb.addItem(getString("ComboBoxDemo.preset2"));
|
|
252 |
cb.addItem(getString("ComboBoxDemo.preset3"));
|
|
253 |
cb.addItem(getString("ComboBoxDemo.preset4"));
|
|
254 |
cb.addItem(getString("ComboBoxDemo.preset5"));
|
|
255 |
cb.addItem(getString("ComboBoxDemo.preset6"));
|
|
256 |
cb.addItem(getString("ComboBoxDemo.preset7"));
|
|
257 |
cb.addItem(getString("ComboBoxDemo.preset8"));
|
|
258 |
cb.addItem(getString("ComboBoxDemo.preset9"));
|
|
259 |
cb.addItem(getString("ComboBoxDemo.preset10"));
|
|
260 |
cb.addActionListener(this);
|
|
261 |
return cb;
|
|
262 |
}
|
|
263 |
|
|
264 |
void fillComboBox(JComboBox cb) {
|
|
265 |
cb.addItem(getString("ComboBoxDemo.brent"));
|
|
266 |
cb.addItem(getString("ComboBoxDemo.georges"));
|
|
267 |
cb.addItem(getString("ComboBoxDemo.hans"));
|
|
268 |
cb.addItem(getString("ComboBoxDemo.howard"));
|
|
269 |
cb.addItem(getString("ComboBoxDemo.james"));
|
|
270 |
cb.addItem(getString("ComboBoxDemo.jeff"));
|
|
271 |
cb.addItem(getString("ComboBoxDemo.jon"));
|
|
272 |
cb.addItem(getString("ComboBoxDemo.lara"));
|
|
273 |
cb.addItem(getString("ComboBoxDemo.larry"));
|
|
274 |
cb.addItem(getString("ComboBoxDemo.lisa"));
|
|
275 |
cb.addItem(getString("ComboBoxDemo.michael"));
|
|
276 |
cb.addItem(getString("ComboBoxDemo.philip"));
|
|
277 |
cb.addItem(getString("ComboBoxDemo.scott"));
|
|
278 |
}
|
|
279 |
|
|
280 |
public void actionPerformed(ActionEvent e) {
|
|
281 |
if(e.getSource() == hairCB) {
|
|
282 |
String name = (String) parts.get((String) hairCB.getSelectedItem());
|
|
283 |
face.setHair((ImageIcon) parts.get(name + "hair"));
|
|
284 |
faceLabel.repaint();
|
|
285 |
} else if(e.getSource() == eyesCB) {
|
|
286 |
String name = (String) parts.get((String) eyesCB.getSelectedItem());
|
|
287 |
face.setEyes((ImageIcon) parts.get(name + "eyes"));
|
|
288 |
faceLabel.repaint();
|
|
289 |
} else if(e.getSource() == mouthCB) {
|
|
290 |
String name = (String) parts.get((String) mouthCB.getSelectedItem());
|
|
291 |
face.setMouth((ImageIcon) parts.get(name + "mouth"));
|
|
292 |
faceLabel.repaint();
|
|
293 |
} else if(e.getSource() == presetCB) {
|
|
294 |
String hair = null;
|
|
295 |
String eyes = null;
|
|
296 |
String mouth = null;
|
|
297 |
switch(presetCB.getSelectedIndex()) {
|
|
298 |
case 0:
|
|
299 |
hair = (String) parts.get("philip");
|
|
300 |
eyes = (String) parts.get("howard");
|
|
301 |
mouth = (String) parts.get("jeff");
|
|
302 |
break;
|
|
303 |
case 1:
|
|
304 |
hair = (String) parts.get("jeff");
|
|
305 |
eyes = (String) parts.get("larry");
|
|
306 |
mouth = (String) parts.get("philip");
|
|
307 |
break;
|
|
308 |
case 2:
|
|
309 |
hair = (String) parts.get("howard");
|
|
310 |
eyes = (String) parts.get("scott");
|
|
311 |
mouth = (String) parts.get("hans");
|
|
312 |
break;
|
|
313 |
case 3:
|
|
314 |
hair = (String) parts.get("philip");
|
|
315 |
eyes = (String) parts.get("jeff");
|
|
316 |
mouth = (String) parts.get("hans");
|
|
317 |
break;
|
|
318 |
case 4:
|
|
319 |
hair = (String) parts.get("brent");
|
|
320 |
eyes = (String) parts.get("jon");
|
|
321 |
mouth = (String) parts.get("scott");
|
|
322 |
break;
|
|
323 |
case 5:
|
|
324 |
hair = (String) parts.get("lara");
|
|
325 |
eyes = (String) parts.get("larry");
|
|
326 |
mouth = (String) parts.get("lisa");
|
|
327 |
break;
|
|
328 |
case 6:
|
|
329 |
hair = (String) parts.get("james");
|
|
330 |
eyes = (String) parts.get("philip");
|
|
331 |
mouth = (String) parts.get("michael");
|
|
332 |
break;
|
|
333 |
case 7:
|
|
334 |
hair = (String) parts.get("philip");
|
|
335 |
eyes = (String) parts.get("lisa");
|
|
336 |
mouth = (String) parts.get("brent");
|
|
337 |
break;
|
|
338 |
case 8:
|
|
339 |
hair = (String) parts.get("james");
|
|
340 |
eyes = (String) parts.get("philip");
|
|
341 |
mouth = (String) parts.get("jon");
|
|
342 |
break;
|
|
343 |
case 9:
|
|
344 |
hair = (String) parts.get("lara");
|
|
345 |
eyes = (String) parts.get("jon");
|
|
346 |
mouth = (String) parts.get("scott");
|
|
347 |
break;
|
|
348 |
}
|
|
349 |
if(hair != null) {
|
|
350 |
hairCB.setSelectedItem(hair);
|
|
351 |
eyesCB.setSelectedItem(eyes);
|
|
352 |
mouthCB.setSelectedItem(mouth);
|
|
353 |
faceLabel.repaint();
|
|
354 |
}
|
|
355 |
}
|
|
356 |
}
|
|
357 |
|
|
358 |
class Face implements Icon {
|
|
359 |
ImageIcon hair;
|
|
360 |
ImageIcon eyes;
|
|
361 |
ImageIcon mouth;
|
|
362 |
|
|
363 |
void setHair(ImageIcon i) {
|
|
364 |
hair = i;
|
|
365 |
}
|
|
366 |
|
|
367 |
void setEyes(ImageIcon i) {
|
|
368 |
eyes = i;
|
|
369 |
}
|
|
370 |
|
|
371 |
void setMouth(ImageIcon i) {
|
|
372 |
mouth = i;
|
|
373 |
}
|
|
374 |
|
|
375 |
public void paintIcon(Component c, Graphics g, int x, int y) {
|
|
376 |
int height = y;
|
|
377 |
x = c.getWidth()/2 - getIconWidth()/2;
|
|
378 |
|
|
379 |
if(hair != null) {
|
|
380 |
hair.paintIcon(c, g, x, height); height += hair.getIconHeight();
|
|
381 |
}
|
|
382 |
|
|
383 |
if(eyes != null) {
|
|
384 |
eyes.paintIcon(c, g, x, height); height += eyes.getIconHeight();
|
|
385 |
}
|
|
386 |
|
|
387 |
if(mouth != null) {
|
|
388 |
mouth.paintIcon(c, g, x, height);
|
|
389 |
}
|
|
390 |
}
|
|
391 |
|
|
392 |
public int getIconWidth() {
|
|
393 |
return 344;
|
|
394 |
}
|
|
395 |
|
|
396 |
public int getIconHeight() {
|
|
397 |
return 455;
|
|
398 |
}
|
|
399 |
}
|
|
400 |
}
|