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 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; 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"); | 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 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; 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"); |