ComboBoxDemo.java revision 13978:1993af50385d
1/* 2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package com.sun.swingset3.demos.combobox; 24 25import java.awt.*; 26import java.awt.event.ActionEvent; 27import java.awt.event.ActionListener; 28import java.util.HashMap; 29import java.util.Map; 30import javax.accessibility.AccessibleRelation; 31import javax.swing.*; 32import javax.swing.border.BevelBorder; 33 34import com.sun.swingset3.demos.ResourceManager; 35import com.sun.swingset3.DemoProperties; 36 37/** 38 * JComboBox Demo 39 * 40 * @author Jeff Dinkins 41 * @version 1.13 11/17/05 42 */ 43@DemoProperties( 44 value = "JComboBox Demo", 45 category = "Controls", 46 description = "Demonstrates JComboBox, a control which allows the user to make a selection from a popup list", 47 sourceFiles = { 48 "com/sun/swingset3/demos/combobox/ComboBoxDemo.java", 49 "com/sun/swingset3/demos/ResourceManager.java", 50 "com/sun/swingset3/demos/combobox/resources/ComboBoxDemo.properties", 51 "com/sun/swingset3/demos/combobox/resources/images/brenteyes.jpg", 52 "com/sun/swingset3/demos/combobox/resources/images/brenthair.jpg", 53 "com/sun/swingset3/demos/combobox/resources/images/brentmouth.jpg", 54 "com/sun/swingset3/demos/combobox/resources/images/ComboBoxDemo.gif", 55 "com/sun/swingset3/demos/combobox/resources/images/georgeseyes.jpg", 56 "com/sun/swingset3/demos/combobox/resources/images/georgeshair.jpg", 57 "com/sun/swingset3/demos/combobox/resources/images/georgesmouth.jpg", 58 "com/sun/swingset3/demos/combobox/resources/images/hanseyes.jpg", 59 "com/sun/swingset3/demos/combobox/resources/images/hanshair.jpg", 60 "com/sun/swingset3/demos/combobox/resources/images/hansmouth.jpg", 61 "com/sun/swingset3/demos/combobox/resources/images/howardeyes.jpg", 62 "com/sun/swingset3/demos/combobox/resources/images/howardhair.jpg", 63 "com/sun/swingset3/demos/combobox/resources/images/howardmouth.jpg", 64 "com/sun/swingset3/demos/combobox/resources/images/jameseyes.jpg", 65 "com/sun/swingset3/demos/combobox/resources/images/jameshair.jpg", 66 "com/sun/swingset3/demos/combobox/resources/images/jamesmouth.jpg", 67 "com/sun/swingset3/demos/combobox/resources/images/jeffeyes.jpg", 68 "com/sun/swingset3/demos/combobox/resources/images/jeffhair.jpg", 69 "com/sun/swingset3/demos/combobox/resources/images/jeffmouth.jpg", 70 "com/sun/swingset3/demos/combobox/resources/images/joneyes.jpg", 71 "com/sun/swingset3/demos/combobox/resources/images/jonhair.jpg", 72 "com/sun/swingset3/demos/combobox/resources/images/jonmouth.jpg", 73 "com/sun/swingset3/demos/combobox/resources/images/laraeyes.jpg", 74 "com/sun/swingset3/demos/combobox/resources/images/larahair.jpg", 75 "com/sun/swingset3/demos/combobox/resources/images/laramouth.jpg", 76 "com/sun/swingset3/demos/combobox/resources/images/larryeyes.jpg", 77 "com/sun/swingset3/demos/combobox/resources/images/larryhair.jpg", 78 "com/sun/swingset3/demos/combobox/resources/images/larrymouth.jpg", 79 "com/sun/swingset3/demos/combobox/resources/images/lisaeyes.jpg", 80 "com/sun/swingset3/demos/combobox/resources/images/lisahair.jpg", 81 "com/sun/swingset3/demos/combobox/resources/images/lisamouth.jpg", 82 "com/sun/swingset3/demos/combobox/resources/images/michaeleyes.jpg", 83 "com/sun/swingset3/demos/combobox/resources/images/michaelhair.jpg", 84 "com/sun/swingset3/demos/combobox/resources/images/michaelmouth.jpg", 85 "com/sun/swingset3/demos/combobox/resources/images/philipeyes.jpg", 86 "com/sun/swingset3/demos/combobox/resources/images/philiphair.jpg", 87 "com/sun/swingset3/demos/combobox/resources/images/philipmouth.jpg", 88 "com/sun/swingset3/demos/combobox/resources/images/scotteyes.jpg", 89 "com/sun/swingset3/demos/combobox/resources/images/scotthair.jpg", 90 "com/sun/swingset3/demos/combobox/resources/images/scottmouth.jpg" 91 } 92) 93public class ComboBoxDemo extends JPanel implements ActionListener { 94 95 private static final Dimension VGAP15 = new Dimension(1, 15); 96 private static final Dimension HGAP20 = new Dimension(20, 1); 97 private static final Dimension VGAP20 = new Dimension(1, 20); 98 private static final Dimension HGAP30 = new Dimension(30, 1); 99 private static final Dimension VGAP30 = new Dimension(1, 30); 100 101 private final ResourceManager resourceManager = new ResourceManager(this.getClass()); 102 public static final String DEMO_TITLE = ComboBoxDemo.class.getAnnotation(DemoProperties.class).value(); 103 104 private Face face; 105 private JLabel faceLabel; 106 107 private JComboBox<String> hairCB; 108 private JComboBox<String> eyesCB; 109 private JComboBox<String> mouthCB; 110 111 private JComboBox<String> presetCB; 112 113 private final Map<String, Object> parts = new HashMap<>(); 114 115 /** 116 * main method allows us to run as a standalone demo. 117 * 118 * @param args 119 */ 120 public static void main(String[] args) { 121 JFrame frame = new JFrame(DEMO_TITLE); 122 123 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 124 frame.getContentPane().add(new ComboBoxDemo()); 125 frame.setPreferredSize(new Dimension(800, 600)); 126 frame.pack(); 127 frame.setLocationRelativeTo(null); 128 frame.setVisible(true); 129 } 130 131 /** 132 * ComboBoxDemo Constructor 133 */ 134 public ComboBoxDemo() { 135 createComboBoxDemo(); 136 } 137 138 private void createComboBoxDemo() { 139 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 140 141 JPanel innerPanel = new JPanel(); 142 innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS)); 143 144 add(Box.createRigidArea(VGAP20)); 145 add(innerPanel); 146 add(Box.createRigidArea(VGAP20)); 147 148 innerPanel.add(Box.createRigidArea(HGAP20)); 149 150 // Create a panel to hold buttons 151 JPanel comboBoxPanel = new JPanel() { 152 @Override 153 public Dimension getMaximumSize() { 154 return new Dimension(getPreferredSize().width, super.getMaximumSize().height); 155 } 156 }; 157 comboBoxPanel.setLayout(new BoxLayout(comboBoxPanel, BoxLayout.Y_AXIS)); 158 159 comboBoxPanel.add(Box.createRigidArea(VGAP15)); 160 161 JLabel l = (JLabel) comboBoxPanel.add(new JLabel(resourceManager.getString("ComboBoxDemo.presets"))); 162 l.setAlignmentX(JLabel.LEFT_ALIGNMENT); 163 comboBoxPanel.add(presetCB = createPresetComboBox()); 164 presetCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT); 165 l.setLabelFor(presetCB); 166 comboBoxPanel.add(Box.createRigidArea(VGAP30)); 167 168 l = (JLabel) comboBoxPanel.add(new JLabel(resourceManager.getString("ComboBoxDemo.hair_description"))); 169 l.setAlignmentX(JLabel.LEFT_ALIGNMENT); 170 comboBoxPanel.add(hairCB = createHairComboBox()); 171 hairCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT); 172 l.setLabelFor(hairCB); 173 comboBoxPanel.add(Box.createRigidArea(VGAP15)); 174 175 l = (JLabel) comboBoxPanel.add(new JLabel(resourceManager.getString("ComboBoxDemo.eyes_description"))); 176 l.setAlignmentX(JLabel.LEFT_ALIGNMENT); 177 comboBoxPanel.add(eyesCB = createEyesComboBox()); 178 eyesCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT); 179 l.setLabelFor(eyesCB); 180 comboBoxPanel.add(Box.createRigidArea(VGAP15)); 181 182 l = (JLabel) comboBoxPanel.add(new JLabel(resourceManager.getString("ComboBoxDemo.mouth_description"))); 183 l.setAlignmentX(JLabel.LEFT_ALIGNMENT); 184 comboBoxPanel.add(mouthCB = createMouthComboBox()); 185 186 mouthCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT); 187 l.setLabelFor(mouthCB); 188 comboBoxPanel.add(Box.createRigidArea(VGAP15)); 189 190 // Fill up the remaining space 191 comboBoxPanel.add(new JPanel(new BorderLayout())); 192 193 // Create and place the Face. 194 face = new Face(); 195 JPanel facePanel = new JPanel(); 196 facePanel.setLayout(new BorderLayout()); 197 facePanel.setBorder(new BevelBorder(BevelBorder.LOWERED)); 198 199 faceLabel = new JLabel(face); 200 facePanel.add(faceLabel, BorderLayout.CENTER); 201 // Indicate that the face panel is controlled by the hair, eyes and 202 // mouth combo boxes. 203 Object[] controlledByObjects = new Object[3]; 204 controlledByObjects[0] = hairCB; 205 controlledByObjects[1] = eyesCB; 206 controlledByObjects[2] = mouthCB; 207 AccessibleRelation controlledByRelation 208 = new AccessibleRelation(AccessibleRelation.CONTROLLED_BY_PROPERTY, 209 controlledByObjects); 210 facePanel.getAccessibleContext().getAccessibleRelationSet().add(controlledByRelation); 211 212 // Indicate that the hair, eyes and mouth combo boxes are controllers 213 // for the face panel. 214 AccessibleRelation controllerForRelation 215 = new AccessibleRelation(AccessibleRelation.CONTROLLER_FOR_PROPERTY, 216 facePanel); 217 hairCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation); 218 eyesCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation); 219 mouthCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation); 220 221 // add buttons and image panels to inner panel 222 innerPanel.add(comboBoxPanel); 223 innerPanel.add(Box.createRigidArea(HGAP30)); 224 innerPanel.add(facePanel); 225 innerPanel.add(Box.createRigidArea(HGAP20)); 226 227 // load up the face parts 228 addFace("brent", resourceManager.getString("ComboBoxDemo.brent")); 229 addFace("georges", resourceManager.getString("ComboBoxDemo.georges")); 230 addFace("hans", resourceManager.getString("ComboBoxDemo.hans")); 231 addFace("howard", resourceManager.getString("ComboBoxDemo.howard")); 232 addFace("james", resourceManager.getString("ComboBoxDemo.james")); 233 addFace("jeff", resourceManager.getString("ComboBoxDemo.jeff")); 234 addFace("jon", resourceManager.getString("ComboBoxDemo.jon")); 235 addFace("lara", resourceManager.getString("ComboBoxDemo.lara")); 236 addFace("larry", resourceManager.getString("ComboBoxDemo.larry")); 237 addFace("lisa", resourceManager.getString("ComboBoxDemo.lisa")); 238 addFace("michael", resourceManager.getString("ComboBoxDemo.michael")); 239 addFace("philip", resourceManager.getString("ComboBoxDemo.philip")); 240 addFace("scott", resourceManager.getString("ComboBoxDemo.scott")); 241 242 // set the default face 243 presetCB.setSelectedIndex(0); 244 } 245 246 private void addFace(String name, String i18n_name) { 247 ImageIcon i; 248 String i18n_hair = resourceManager.getString("ComboBoxDemo.hair"); 249 String i18n_eyes = resourceManager.getString("ComboBoxDemo.eyes"); 250 String i18n_mouth = resourceManager.getString("ComboBoxDemo.mouth"); 251 252 parts.put(i18n_name, name); // i18n name lookup 253 parts.put(name, i18n_name); // reverse name lookup 254 255 i = resourceManager.createImageIcon(name + "hair.jpg", i18n_name + i18n_hair); 256 parts.put(name + "hair", i); 257 258 i = resourceManager.createImageIcon(name + "eyes.jpg", i18n_name + i18n_eyes); 259 parts.put(name + "eyes", i); 260 261 i = resourceManager.createImageIcon(name + "mouth.jpg", i18n_name + i18n_mouth); 262 parts.put(name + "mouth", i); 263 } 264 265 private JComboBox<String> createHairComboBox() { 266 JComboBox<String> cb = new JComboBox<>(); 267 fillComboBox(cb); 268 cb.addActionListener(this); 269 return cb; 270 } 271 272 private JComboBox<String> createEyesComboBox() { 273 JComboBox<String> cb = new JComboBox<>(); 274 fillComboBox(cb); 275 cb.addActionListener(this); 276 return cb; 277 } 278 279 private JComboBox<String> createMouthComboBox() { 280 JComboBox<String> cb = new JComboBox<>(); 281 fillComboBox(cb); 282 cb.addActionListener(this); 283 return cb; 284 } 285 286 private JComboBox<String> createPresetComboBox() { 287 JComboBox<String> cb = new JComboBox<>(); 288 cb.addItem(resourceManager.getString("ComboBoxDemo.preset1")); 289 cb.addItem(resourceManager.getString("ComboBoxDemo.preset2")); 290 cb.addItem(resourceManager.getString("ComboBoxDemo.preset3")); 291 cb.addItem(resourceManager.getString("ComboBoxDemo.preset4")); 292 cb.addItem(resourceManager.getString("ComboBoxDemo.preset5")); 293 cb.addItem(resourceManager.getString("ComboBoxDemo.preset6")); 294 cb.addItem(resourceManager.getString("ComboBoxDemo.preset7")); 295 cb.addItem(resourceManager.getString("ComboBoxDemo.preset8")); 296 cb.addItem(resourceManager.getString("ComboBoxDemo.preset9")); 297 cb.addItem(resourceManager.getString("ComboBoxDemo.preset10")); 298 cb.addActionListener(this); 299 return cb; 300 } 301 302 private void fillComboBox(JComboBox<String> cb) { 303 cb.addItem(resourceManager.getString("ComboBoxDemo.brent")); 304 cb.addItem(resourceManager.getString("ComboBoxDemo.georges")); 305 cb.addItem(resourceManager.getString("ComboBoxDemo.hans")); 306 cb.addItem(resourceManager.getString("ComboBoxDemo.howard")); 307 cb.addItem(resourceManager.getString("ComboBoxDemo.james")); 308 cb.addItem(resourceManager.getString("ComboBoxDemo.jeff")); 309 cb.addItem(resourceManager.getString("ComboBoxDemo.jon")); 310 cb.addItem(resourceManager.getString("ComboBoxDemo.lara")); 311 cb.addItem(resourceManager.getString("ComboBoxDemo.larry")); 312 cb.addItem(resourceManager.getString("ComboBoxDemo.lisa")); 313 cb.addItem(resourceManager.getString("ComboBoxDemo.michael")); 314 cb.addItem(resourceManager.getString("ComboBoxDemo.philip")); 315 cb.addItem(resourceManager.getString("ComboBoxDemo.scott")); 316 } 317 318 @Override 319 public void actionPerformed(ActionEvent e) { 320 if (e.getSource() == hairCB) { 321 String name = (String) parts.get(hairCB.getSelectedItem()); 322 face.setHair((ImageIcon) parts.get(name + "hair")); 323 faceLabel.repaint(); 324 } else if (e.getSource() == eyesCB) { 325 String name = (String) parts.get(eyesCB.getSelectedItem()); 326 face.setEyes((ImageIcon) parts.get(name + "eyes")); 327 faceLabel.repaint(); 328 } else if (e.getSource() == mouthCB) { 329 String name = (String) parts.get(mouthCB.getSelectedItem()); 330 face.setMouth((ImageIcon) parts.get(name + "mouth")); 331 faceLabel.repaint(); 332 } else if (e.getSource() == presetCB) { 333 String hair = null; 334 String eyes = null; 335 String mouth = null; 336 switch (presetCB.getSelectedIndex()) { 337 case 0: 338 hair = (String) parts.get("philip"); 339 eyes = (String) parts.get("howard"); 340 mouth = (String) parts.get("jeff"); 341 break; 342 case 1: 343 hair = (String) parts.get("jeff"); 344 eyes = (String) parts.get("larry"); 345 mouth = (String) parts.get("philip"); 346 break; 347 case 2: 348 hair = (String) parts.get("howard"); 349 eyes = (String) parts.get("scott"); 350 mouth = (String) parts.get("hans"); 351 break; 352 case 3: 353 hair = (String) parts.get("philip"); 354 eyes = (String) parts.get("jeff"); 355 mouth = (String) parts.get("hans"); 356 break; 357 case 4: 358 hair = (String) parts.get("brent"); 359 eyes = (String) parts.get("jon"); 360 mouth = (String) parts.get("scott"); 361 break; 362 case 5: 363 hair = (String) parts.get("lara"); 364 eyes = (String) parts.get("larry"); 365 mouth = (String) parts.get("lisa"); 366 break; 367 case 6: 368 hair = (String) parts.get("james"); 369 eyes = (String) parts.get("philip"); 370 mouth = (String) parts.get("michael"); 371 break; 372 case 7: 373 hair = (String) parts.get("philip"); 374 eyes = (String) parts.get("lisa"); 375 mouth = (String) parts.get("brent"); 376 break; 377 case 8: 378 hair = (String) parts.get("james"); 379 eyes = (String) parts.get("philip"); 380 mouth = (String) parts.get("jon"); 381 break; 382 case 9: 383 hair = (String) parts.get("lara"); 384 eyes = (String) parts.get("jon"); 385 mouth = (String) parts.get("scott"); 386 break; 387 } 388 if (hair != null) { 389 hairCB.setSelectedItem(hair); 390 eyesCB.setSelectedItem(eyes); 391 mouthCB.setSelectedItem(mouth); 392 faceLabel.repaint(); 393 } 394 } 395 } 396 397 private static class Face implements Icon { 398 399 private ImageIcon hair; 400 private ImageIcon eyes; 401 private ImageIcon mouth; 402 403 void setHair(ImageIcon i) { 404 hair = i; 405 } 406 407 void setEyes(ImageIcon i) { 408 eyes = i; 409 } 410 411 void setMouth(ImageIcon i) { 412 mouth = i; 413 } 414 415 @Override 416 public void paintIcon(Component c, Graphics g, int x, int y) { 417 int height = y; 418 x = c.getWidth() / 2 - getIconWidth() / 2; 419 420 if (hair != null) { 421 hair.paintIcon(c, g, x, height); 422 height += hair.getIconHeight(); 423 } 424 425 if (eyes != null) { 426 eyes.paintIcon(c, g, x, height); 427 height += eyes.getIconHeight(); 428 } 429 430 if (mouth != null) { 431 mouth.paintIcon(c, g, x, height); 432 } 433 } 434 435 @Override 436 public int getIconWidth() { 437 return 344; 438 } 439 440 @Override 441 public int getIconHeight() { 442 return 455; 443 } 444 } 445} 446