Path: blob/master/src/demo/share/jfc/SwingSet2/ComboBoxDemo.java
41149 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.border.*;37import javax.swing.colorchooser.*;38import javax.swing.filechooser.*;39import javax.accessibility.*;4041import java.awt.*;42import java.awt.event.*;43import java.beans.*;44import java.util.*;45import java.io.*;46import java.applet.*;47import java.net.*;4849/**50* JComboBox Demo51*52* @author Jeff Dinkins53*/54public class ComboBoxDemo extends DemoModule implements ActionListener {5556Face face;57JLabel faceLabel;5859JComboBox<?> hairCB;60JComboBox<?> eyesCB;61JComboBox<?> mouthCB;6263JComboBox<?> presetCB;6465Hashtable<String, Object> parts = new Hashtable<>();6667/**68* main method allows us to run as a standalone demo.69*/70public static void main(String[] args) {71ComboBoxDemo demo = new ComboBoxDemo(null);72demo.mainImpl();73}7475/**76* ComboBoxDemo Constructor77*/78public ComboBoxDemo(SwingSet2 swingset) {79// Set the title for this demo, and an icon used to represent this80// demo inside the SwingSet2 app.81super(swingset, "ComboBoxDemo", "toolbar/JComboBox.gif");8283createComboBoxDemo();84}8586public void createComboBoxDemo() {87JPanel demo = getDemoPanel();8889JPanel demoPanel = getDemoPanel();90demoPanel.setLayout(new BoxLayout(demoPanel, BoxLayout.Y_AXIS));9192JPanel innerPanel = new JPanel();93innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));9495demoPanel.add(Box.createRigidArea(VGAP20));96demoPanel.add(innerPanel);97demoPanel.add(Box.createRigidArea(VGAP20));9899innerPanel.add(Box.createRigidArea(HGAP20));100101// Create a panel to hold buttons102JPanel comboBoxPanel = new JPanel() {103public Dimension getMaximumSize() {104return new Dimension(getPreferredSize().width, super.getMaximumSize().height);105}106};107comboBoxPanel.setLayout(new BoxLayout(comboBoxPanel, BoxLayout.Y_AXIS));108109comboBoxPanel.add(Box.createRigidArea(VGAP15));110111JLabel l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.presets")));112l.setAlignmentX(JLabel.LEFT_ALIGNMENT);113presetCB = createPresetComboBox();114presetCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);115l.setLabelFor(presetCB);116comboBoxPanel.add(presetCB);117comboBoxPanel.add(Box.createRigidArea(VGAP30));118119l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.hair_description")));120l.setAlignmentX(JLabel.LEFT_ALIGNMENT);121hairCB = createHairComboBox();122hairCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);123l.setLabelFor(hairCB);124comboBoxPanel.add(hairCB);125comboBoxPanel.add(Box.createRigidArea(VGAP15));126127l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.eyes_description")));128l.setAlignmentX(JLabel.LEFT_ALIGNMENT);129eyesCB = createEyesComboBox();130eyesCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);131l.setLabelFor(eyesCB);132comboBoxPanel.add(eyesCB);133comboBoxPanel.add(Box.createRigidArea(VGAP15));134135l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.mouth_description")));136l.setAlignmentX(JLabel.LEFT_ALIGNMENT);137mouthCB = (JComboBox<?>) comboBoxPanel.add(createMouthComboBox());138mouthCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT);139l.setLabelFor(mouthCB);140comboBoxPanel.add(Box.createRigidArea(VGAP15));141142// Fill up the remaining space143comboBoxPanel.add(new JPanel(new BorderLayout()));144145// Create and place the Face.146147face = new Face();148JPanel facePanel = new JPanel();149facePanel.setLayout(new BorderLayout());150facePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));151152faceLabel = new JLabel(face);153facePanel.add(faceLabel, BorderLayout.CENTER);154// Indicate that the face panel is controlled by the hair, eyes and155// mouth combo boxes.156Object [] controlledByObjects = new Object[3];157controlledByObjects[0] = hairCB;158controlledByObjects[1] = eyesCB;159controlledByObjects[2] = mouthCB;160AccessibleRelation controlledByRelation =161new AccessibleRelation(AccessibleRelation.CONTROLLED_BY_PROPERTY,162controlledByObjects);163facePanel.getAccessibleContext().getAccessibleRelationSet().add(controlledByRelation);164165// Indicate that the hair, eyes and mouth combo boxes are controllers166// for the face panel.167AccessibleRelation controllerForRelation =168new AccessibleRelation(AccessibleRelation.CONTROLLER_FOR_PROPERTY,169facePanel);170hairCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);171eyesCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);172mouthCB.getAccessibleContext().getAccessibleRelationSet().add(controllerForRelation);173174// add buttons and image panels to inner panel175innerPanel.add(comboBoxPanel);176innerPanel.add(Box.createRigidArea(HGAP30));177innerPanel.add(facePanel);178innerPanel.add(Box.createRigidArea(HGAP20));179180// load up the face parts181addFace("brent", getString("ComboBoxDemo.brent"));182addFace("georges", getString("ComboBoxDemo.georges"));183addFace("hans", getString("ComboBoxDemo.hans"));184addFace("howard", getString("ComboBoxDemo.howard"));185addFace("james", getString("ComboBoxDemo.james"));186addFace("jeff", getString("ComboBoxDemo.jeff"));187addFace("jon", getString("ComboBoxDemo.jon"));188addFace("lara", getString("ComboBoxDemo.lara"));189addFace("larry", getString("ComboBoxDemo.larry"));190addFace("lisa", getString("ComboBoxDemo.lisa"));191addFace("michael", getString("ComboBoxDemo.michael"));192addFace("philip", getString("ComboBoxDemo.philip"));193addFace("scott", getString("ComboBoxDemo.scott"));194195// set the default face196presetCB.setSelectedIndex(0);197}198199void addFace(String name, String i18n_name) {200ImageIcon i;201String i18n_hair = getString("ComboBoxDemo.hair");202String i18n_eyes = getString("ComboBoxDemo.eyes");203String i18n_mouth = getString("ComboBoxDemo.mouth");204205parts.put(i18n_name, name); // i18n name lookup206parts.put(name, i18n_name); // reverse name lookup207208i = createImageIcon("combobox/" + name + "hair.jpg", i18n_name + i18n_hair);209parts.put(name + "hair", i);210211i = createImageIcon("combobox/" + name + "eyes.jpg", i18n_name + i18n_eyes);212parts.put(name + "eyes", i);213214i = createImageIcon("combobox/" + name + "mouth.jpg", i18n_name + i18n_mouth);215parts.put(name + "mouth", i);216}217218Face getFace() {219return face;220}221222JComboBox<String> createHairComboBox() {223JComboBox<String> cb = new JComboBox<>();224fillComboBox(cb);225cb.addActionListener(this);226return cb;227}228229JComboBox<String> createEyesComboBox() {230JComboBox<String> cb = new JComboBox<>();231fillComboBox(cb);232cb.addActionListener(this);233return cb;234}235236JComboBox<String> createNoseComboBox() {237JComboBox<String> cb = new JComboBox<>();238fillComboBox(cb);239cb.addActionListener(this);240return cb;241}242243JComboBox<String> createMouthComboBox() {244JComboBox<String> cb = new JComboBox<>();245fillComboBox(cb);246cb.addActionListener(this);247return cb;248}249250JComboBox<String> createPresetComboBox() {251JComboBox<String> cb = new JComboBox<>();252cb.addItem(getString("ComboBoxDemo.preset1"));253cb.addItem(getString("ComboBoxDemo.preset2"));254cb.addItem(getString("ComboBoxDemo.preset3"));255cb.addItem(getString("ComboBoxDemo.preset4"));256cb.addItem(getString("ComboBoxDemo.preset5"));257cb.addItem(getString("ComboBoxDemo.preset6"));258cb.addItem(getString("ComboBoxDemo.preset7"));259cb.addItem(getString("ComboBoxDemo.preset8"));260cb.addItem(getString("ComboBoxDemo.preset9"));261cb.addItem(getString("ComboBoxDemo.preset10"));262cb.addActionListener(this);263return cb;264}265266void fillComboBox(JComboBox<String> cb) {267cb.addItem(getString("ComboBoxDemo.brent"));268cb.addItem(getString("ComboBoxDemo.georges"));269cb.addItem(getString("ComboBoxDemo.hans"));270cb.addItem(getString("ComboBoxDemo.howard"));271cb.addItem(getString("ComboBoxDemo.james"));272cb.addItem(getString("ComboBoxDemo.jeff"));273cb.addItem(getString("ComboBoxDemo.jon"));274cb.addItem(getString("ComboBoxDemo.lara"));275cb.addItem(getString("ComboBoxDemo.larry"));276cb.addItem(getString("ComboBoxDemo.lisa"));277cb.addItem(getString("ComboBoxDemo.michael"));278cb.addItem(getString("ComboBoxDemo.philip"));279cb.addItem(getString("ComboBoxDemo.scott"));280}281282public void actionPerformed(ActionEvent e) {283if(e.getSource() == hairCB) {284String name = (String) parts.get(hairCB.getSelectedItem());285face.setHair((ImageIcon) parts.get(name + "hair"));286faceLabel.repaint();287} else if(e.getSource() == eyesCB) {288String name = (String) parts.get(eyesCB.getSelectedItem());289face.setEyes((ImageIcon) parts.get(name + "eyes"));290faceLabel.repaint();291} else if(e.getSource() == mouthCB) {292String name = (String) parts.get(mouthCB.getSelectedItem());293face.setMouth((ImageIcon) parts.get(name + "mouth"));294faceLabel.repaint();295} else if(e.getSource() == presetCB) {296String hair = null;297String eyes = null;298String mouth = null;299switch(presetCB.getSelectedIndex()) {300case 0:301hair = (String) parts.get("philip");302eyes = (String) parts.get("howard");303mouth = (String) parts.get("jeff");304break;305case 1:306hair = (String) parts.get("jeff");307eyes = (String) parts.get("larry");308mouth = (String) parts.get("philip");309break;310case 2:311hair = (String) parts.get("howard");312eyes = (String) parts.get("scott");313mouth = (String) parts.get("hans");314break;315case 3:316hair = (String) parts.get("philip");317eyes = (String) parts.get("jeff");318mouth = (String) parts.get("hans");319break;320case 4:321hair = (String) parts.get("brent");322eyes = (String) parts.get("jon");323mouth = (String) parts.get("scott");324break;325case 5:326hair = (String) parts.get("lara");327eyes = (String) parts.get("larry");328mouth = (String) parts.get("lisa");329break;330case 6:331hair = (String) parts.get("james");332eyes = (String) parts.get("philip");333mouth = (String) parts.get("michael");334break;335case 7:336hair = (String) parts.get("philip");337eyes = (String) parts.get("lisa");338mouth = (String) parts.get("brent");339break;340case 8:341hair = (String) parts.get("james");342eyes = (String) parts.get("philip");343mouth = (String) parts.get("jon");344break;345case 9:346hair = (String) parts.get("lara");347eyes = (String) parts.get("jon");348mouth = (String) parts.get("scott");349break;350}351if(hair != null) {352hairCB.setSelectedItem(hair);353eyesCB.setSelectedItem(eyes);354mouthCB.setSelectedItem(mouth);355faceLabel.repaint();356}357}358}359360class Face implements Icon {361ImageIcon hair;362ImageIcon eyes;363ImageIcon mouth;364365void setHair(ImageIcon i) {366hair = i;367}368369void setEyes(ImageIcon i) {370eyes = i;371}372373void setMouth(ImageIcon i) {374mouth = i;375}376377public void paintIcon(Component c, Graphics g, int x, int y) {378int height = y;379x = c.getWidth()/2 - getIconWidth()/2;380381if(hair != null) {382hair.paintIcon(c, g, x, height); height += hair.getIconHeight();383}384385if(eyes != null) {386eyes.paintIcon(c, g, x, height); height += eyes.getIconHeight();387}388389if(mouth != null) {390mouth.paintIcon(c, g, x, height);391}392}393394public int getIconWidth() {395return 344;396}397398public int getIconHeight() {399return 455;400}401}402}403404405