001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package examples;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022:
023: import javax.swing.*;
024:
025: public class CheckBoxDemo extends JFrame {
026: public static void main(String s[]) {
027: JFrame frame = new CheckBoxDemo();
028: frame.addWindowListener(new WindowAdapter() {
029: public void windowClosing(WindowEvent e) {
030: System.exit(0);
031: }
032: });
033:
034: frame.setContentPane(new CheckBoxDemo());
035: frame.pack();
036: frame.setVisible(true);
037: }
038:
039: public CheckBoxDemo() {
040: setContentPane(new CheckBoxDemoPanel());
041: }
042:
043: public static class CheckBoxDemoPanel extends JPanel {
044: JCheckBox chinButton;
045: JCheckBox glassesButton;
046: JCheckBox hairButton;
047: JCheckBox teethButton;
048:
049: /*
050: * Four accessory choices provide for 16 different
051: * combinations. The image for each combination is
052: * contained in a separate image file whose name indicates
053: * the accessories. The filenames are "geek-XXXX.gif"
054: * where XXXX can be one of the following 16 choices.
055: * The "choices" StringBuffer contains the string that
056: * indicates the current selection and is used to generate
057: * the file name of the image to display.
058:
059: ---- // zero accessories
060:
061: c--- // one accessory
062: -g--
063: --h-
064: ---t
065:
066: cg-- // two accessories
067: c-h-
068: c--t
069: -gh-
070: -g-t
071: --ht
072:
073: -ght // three accessories
074: c-ht
075: cg-t
076: cgh-
077:
078: cght // all accessories
079: */
080:
081: StringBuffer choices;
082: JLabel pictureLabel;
083:
084: public CheckBoxDemoPanel() {
085:
086: // Create the check boxes
087: chinButton = new JCheckBox("Chin");
088: chinButton.setMnemonic(KeyEvent.VK_C);
089: chinButton.setSelected(true);
090:
091: glassesButton = new JCheckBox("Glasses");
092: glassesButton.setMnemonic(KeyEvent.VK_G);
093: glassesButton.setSelected(true);
094:
095: hairButton = new JCheckBox("Hair");
096: hairButton.setMnemonic(KeyEvent.VK_H);
097: hairButton.setSelected(true);
098:
099: teethButton = new JCheckBox("Teeth");
100: teethButton.setMnemonic(KeyEvent.VK_T);
101: teethButton.setSelected(true);
102:
103: // Register a listener for the check boxes.
104: CheckBoxListener myListener = new CheckBoxListener();
105: chinButton.addItemListener(myListener);
106: glassesButton.addItemListener(myListener);
107: hairButton.addItemListener(myListener);
108: teethButton.addItemListener(myListener);
109:
110: // Indicates what's on the geek.
111: choices = new StringBuffer("cght");
112:
113: // Set up the picture label
114: pictureLabel = new JLabel(new ImageIcon(
115: "resources/images/geek/geek-" + choices.toString()
116: + ".gif"));
117: pictureLabel.setToolTipText(choices.toString());
118:
119: // Put the check boxes in a column in a panel
120: JPanel checkPanel = new JPanel();
121: checkPanel.setLayout(new GridLayout(0, 1));
122: checkPanel.add(chinButton);
123: checkPanel.add(glassesButton);
124: checkPanel.add(hairButton);
125: checkPanel.add(teethButton);
126:
127: setLayout(new BorderLayout());
128: add(checkPanel, BorderLayout.WEST);
129: add(pictureLabel, BorderLayout.CENTER);
130: setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
131: }
132:
133: /** Listens to the check boxes. */
134: class CheckBoxListener implements ItemListener {
135: public void itemStateChanged(ItemEvent e) {
136: int index = 0;
137: char c = '-';
138: Object source = e.getItemSelectable();
139:
140: if (source == chinButton) {
141: index = 0;
142: c = 'c';
143: } else if (source == glassesButton) {
144: index = 1;
145: c = 'g';
146: } else if (source == hairButton) {
147: index = 2;
148: c = 'h';
149: } else if (source == teethButton) {
150: index = 3;
151: c = 't';
152: }
153:
154: if (e.getStateChange() == ItemEvent.DESELECTED)
155: c = '-';
156:
157: choices.setCharAt(index, c);
158: pictureLabel.setIcon(new ImageIcon(
159: "resources/images/geek/geek-"
160: + choices.toString() + ".gif"));
161: pictureLabel.setToolTipText(choices.toString());
162: }
163: }
164:
165: }
166: }
|