Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/SwingSet2/ComboBoxDemo.java
41149 views
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<String, Object> 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 = createPresetComboBox();
115
presetCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
116
l.setLabelFor(presetCB);
117
comboBoxPanel.add(presetCB);
118
comboBoxPanel.add(Box.createRigidArea(VGAP30));
119
120
l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.hair_description")));
121
l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
122
hairCB = createHairComboBox();
123
hairCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
124
l.setLabelFor(hairCB);
125
comboBoxPanel.add(hairCB);
126
comboBoxPanel.add(Box.createRigidArea(VGAP15));
127
128
l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.eyes_description")));
129
l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
130
eyesCB = createEyesComboBox();
131
eyesCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
132
l.setLabelFor(eyesCB);
133
comboBoxPanel.add(eyesCB);
134
comboBoxPanel.add(Box.createRigidArea(VGAP15));
135
136
l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.mouth_description")));
137
l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
138
mouthCB = (JComboBox<?>) comboBoxPanel.add(createMouthComboBox());
139
mouthCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
140
l.setLabelFor(mouthCB);
141
comboBoxPanel.add(Box.createRigidArea(VGAP15));
142
143
// Fill up the remaining space
144
comboBoxPanel.add(new JPanel(new BorderLayout()));
145
146
// Create and place the Face.
147
148
face = new Face();
149
JPanel facePanel = new JPanel();
150
facePanel.setLayout(new BorderLayout());
151
facePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
152
153
faceLabel = new JLabel(face);
154
facePanel.add(faceLabel, BorderLayout.CENTER);
155
// Indicate that the face panel is controlled by the hair, eyes and
156
// mouth combo boxes.
157
Object [] controlledByObjects = new Object[3];
158
controlledByObjects[0] = hairCB;
159
controlledByObjects[1] = eyesCB;
160
controlledByObjects[2] = mouthCB;
161
AccessibleRelation controlledByRelation =
162
new AccessibleRelation(AccessibleRelation.CONTROLLED_BY_PROPERTY,
163
controlledByObjects);
164
facePanel.getAccessibleContext().getAccessibleRelationSet().add(controlledByRelation);
165
166
// Indicate that the hair, eyes and mouth combo boxes are controllers
167
// for the face panel.
168
AccessibleRelation controllerForRelation =
169
new AccessibleRelation(AccessibleRelation.CONTROLLER_FOR_PROPERTY,
170
facePanel);
171
hairCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
172
eyesCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
173
mouthCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);
174
175
// add buttons and image panels to inner panel
176
innerPanel.add(comboBoxPanel);
177
innerPanel.add(Box.createRigidArea(HGAP30));
178
innerPanel.add(facePanel);
179
innerPanel.add(Box.createRigidArea(HGAP20));
180
181
// load up the face parts
182
addFace("brent", getString("ComboBoxDemo.brent"));
183
addFace("georges", getString("ComboBoxDemo.georges"));
184
addFace("hans", getString("ComboBoxDemo.hans"));
185
addFace("howard", getString("ComboBoxDemo.howard"));
186
addFace("james", getString("ComboBoxDemo.james"));
187
addFace("jeff", getString("ComboBoxDemo.jeff"));
188
addFace("jon", getString("ComboBoxDemo.jon"));
189
addFace("lara", getString("ComboBoxDemo.lara"));
190
addFace("larry", getString("ComboBoxDemo.larry"));
191
addFace("lisa", getString("ComboBoxDemo.lisa"));
192
addFace("michael", getString("ComboBoxDemo.michael"));
193
addFace("philip", getString("ComboBoxDemo.philip"));
194
addFace("scott", getString("ComboBoxDemo.scott"));
195
196
// set the default face
197
presetCB.setSelectedIndex(0);
198
}
199
200
void addFace(String name, String i18n_name) {
201
ImageIcon i;
202
String i18n_hair = getString("ComboBoxDemo.hair");
203
String i18n_eyes = getString("ComboBoxDemo.eyes");
204
String i18n_mouth = getString("ComboBoxDemo.mouth");
205
206
parts.put(i18n_name, name); // i18n name lookup
207
parts.put(name, i18n_name); // reverse name lookup
208
209
i = createImageIcon("combobox/" + name + "hair.jpg", i18n_name + i18n_hair);
210
parts.put(name + "hair", i);
211
212
i = createImageIcon("combobox/" + name + "eyes.jpg", i18n_name + i18n_eyes);
213
parts.put(name + "eyes", i);
214
215
i = createImageIcon("combobox/" + name + "mouth.jpg", i18n_name + i18n_mouth);
216
parts.put(name + "mouth", i);
217
}
218
219
Face getFace() {
220
return face;
221
}
222
223
JComboBox<String> createHairComboBox() {
224
JComboBox<String> cb = new JComboBox<>();
225
fillComboBox(cb);
226
cb.addActionListener(this);
227
return cb;
228
}
229
230
JComboBox<String> createEyesComboBox() {
231
JComboBox<String> cb = new JComboBox<>();
232
fillComboBox(cb);
233
cb.addActionListener(this);
234
return cb;
235
}
236
237
JComboBox<String> createNoseComboBox() {
238
JComboBox<String> cb = new JComboBox<>();
239
fillComboBox(cb);
240
cb.addActionListener(this);
241
return cb;
242
}
243
244
JComboBox<String> createMouthComboBox() {
245
JComboBox<String> cb = new JComboBox<>();
246
fillComboBox(cb);
247
cb.addActionListener(this);
248
return cb;
249
}
250
251
JComboBox<String> createPresetComboBox() {
252
JComboBox<String> cb = new JComboBox<>();
253
cb.addItem(getString("ComboBoxDemo.preset1"));
254
cb.addItem(getString("ComboBoxDemo.preset2"));
255
cb.addItem(getString("ComboBoxDemo.preset3"));
256
cb.addItem(getString("ComboBoxDemo.preset4"));
257
cb.addItem(getString("ComboBoxDemo.preset5"));
258
cb.addItem(getString("ComboBoxDemo.preset6"));
259
cb.addItem(getString("ComboBoxDemo.preset7"));
260
cb.addItem(getString("ComboBoxDemo.preset8"));
261
cb.addItem(getString("ComboBoxDemo.preset9"));
262
cb.addItem(getString("ComboBoxDemo.preset10"));
263
cb.addActionListener(this);
264
return cb;
265
}
266
267
void fillComboBox(JComboBox<String> cb) {
268
cb.addItem(getString("ComboBoxDemo.brent"));
269
cb.addItem(getString("ComboBoxDemo.georges"));
270
cb.addItem(getString("ComboBoxDemo.hans"));
271
cb.addItem(getString("ComboBoxDemo.howard"));
272
cb.addItem(getString("ComboBoxDemo.james"));
273
cb.addItem(getString("ComboBoxDemo.jeff"));
274
cb.addItem(getString("ComboBoxDemo.jon"));
275
cb.addItem(getString("ComboBoxDemo.lara"));
276
cb.addItem(getString("ComboBoxDemo.larry"));
277
cb.addItem(getString("ComboBoxDemo.lisa"));
278
cb.addItem(getString("ComboBoxDemo.michael"));
279
cb.addItem(getString("ComboBoxDemo.philip"));
280
cb.addItem(getString("ComboBoxDemo.scott"));
281
}
282
283
public void actionPerformed(ActionEvent e) {
284
if(e.getSource() == hairCB) {
285
String name = (String) parts.get(hairCB.getSelectedItem());
286
face.setHair((ImageIcon) parts.get(name + "hair"));
287
faceLabel.repaint();
288
} else if(e.getSource() == eyesCB) {
289
String name = (String) parts.get(eyesCB.getSelectedItem());
290
face.setEyes((ImageIcon) parts.get(name + "eyes"));
291
faceLabel.repaint();
292
} else if(e.getSource() == mouthCB) {
293
String name = (String) parts.get(mouthCB.getSelectedItem());
294
face.setMouth((ImageIcon) parts.get(name + "mouth"));
295
faceLabel.repaint();
296
} else if(e.getSource() == presetCB) {
297
String hair = null;
298
String eyes = null;
299
String mouth = null;
300
switch(presetCB.getSelectedIndex()) {
301
case 0:
302
hair = (String) parts.get("philip");
303
eyes = (String) parts.get("howard");
304
mouth = (String) parts.get("jeff");
305
break;
306
case 1:
307
hair = (String) parts.get("jeff");
308
eyes = (String) parts.get("larry");
309
mouth = (String) parts.get("philip");
310
break;
311
case 2:
312
hair = (String) parts.get("howard");
313
eyes = (String) parts.get("scott");
314
mouth = (String) parts.get("hans");
315
break;
316
case 3:
317
hair = (String) parts.get("philip");
318
eyes = (String) parts.get("jeff");
319
mouth = (String) parts.get("hans");
320
break;
321
case 4:
322
hair = (String) parts.get("brent");
323
eyes = (String) parts.get("jon");
324
mouth = (String) parts.get("scott");
325
break;
326
case 5:
327
hair = (String) parts.get("lara");
328
eyes = (String) parts.get("larry");
329
mouth = (String) parts.get("lisa");
330
break;
331
case 6:
332
hair = (String) parts.get("james");
333
eyes = (String) parts.get("philip");
334
mouth = (String) parts.get("michael");
335
break;
336
case 7:
337
hair = (String) parts.get("philip");
338
eyes = (String) parts.get("lisa");
339
mouth = (String) parts.get("brent");
340
break;
341
case 8:
342
hair = (String) parts.get("james");
343
eyes = (String) parts.get("philip");
344
mouth = (String) parts.get("jon");
345
break;
346
case 9:
347
hair = (String) parts.get("lara");
348
eyes = (String) parts.get("jon");
349
mouth = (String) parts.get("scott");
350
break;
351
}
352
if(hair != null) {
353
hairCB.setSelectedItem(hair);
354
eyesCB.setSelectedItem(eyes);
355
mouthCB.setSelectedItem(mouth);
356
faceLabel.repaint();
357
}
358
}
359
}
360
361
class Face implements Icon {
362
ImageIcon hair;
363
ImageIcon eyes;
364
ImageIcon mouth;
365
366
void setHair(ImageIcon i) {
367
hair = i;
368
}
369
370
void setEyes(ImageIcon i) {
371
eyes = i;
372
}
373
374
void setMouth(ImageIcon i) {
375
mouth = i;
376
}
377
378
public void paintIcon(Component c, Graphics g, int x, int y) {
379
int height = y;
380
x = c.getWidth()/2 - getIconWidth()/2;
381
382
if(hair != null) {
383
hair.paintIcon(c, g, x, height); height += hair.getIconHeight();
384
}
385
386
if(eyes != null) {
387
eyes.paintIcon(c, g, x, height); height += eyes.getIconHeight();
388
}
389
390
if(mouth != null) {
391
mouth.paintIcon(c, g, x, height);
392
}
393
}
394
395
public int getIconWidth() {
396
return 344;
397
}
398
399
public int getIconHeight() {
400
return 455;
401
}
402
}
403
}
404
405