import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CheckBoxDemo extends JPanel { JCheckBox chinButton; JCheckBox glassesButton; JCheckBox hairButton; JCheckBox teethButton; StringBuffer choices; JLabel pictureLabel; public CheckBoxDemo() { // Create the check boxes
chinButton = new JCheckBox("Chin"); chinButton.setMnemonic(KeyEvent.VK_C); chinButton.setSelected(true); glassesButton = new JCheckBox("Glasses"); glassesButton.setMnemonic(KeyEvent.VK_G); glassesButton.setSelected(true); hairButton = new JCheckBox("Hair"); hairButton.setMnemonic(KeyEvent.VK_H); hairButton.setSelected(true);
teethButton = new JCheckBox("Teeth"); teethButton.setMnemonic(KeyEvent.VK_T); teethButton.setSelected(true); // Register a listener for the check boxes. CheckBoxListener myListener = new CheckBoxListener(); chinButton.addItemListener(myListener); glassesButton.addItemListener(myListener); hairButton.addItemListener(myListener); teethButton.addItemListener(myListener); // Indicates what's on the geek. choices = new StringBuffer("cght"); // Set up the picture label pictureLabel = new JLabel(new ImageIcon("images/geek/geek-"+ choices.toString() + ".gif")); pictureLabel.setToolTipText(choices.toString()); // Put the check boxes in a column in a panel JPanel checkPanel = new JPanel(); checkPanel.setLayout(new GridLayout(0, 1)); checkPanel.add(chinButton); checkPanel.add(glassesButton); checkPanel.add(hairButton); checkPanel.add(teethButton); setLayout(new BorderLayout()); add(checkPanel, BorderLayout.WEST); add(pictureLabel, BorderLayout.CENTER); setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); } /** Listens to the check boxes. */
class CheckBoxListener implements ItemListener { public void itemStateChanged(ItemEvent e) { int index = 0; char c = '-'; Object source = e.getItemSelectable(); if (source == chinButton) { index = 0; c = 'c'; } else if (source == glassesButton) { index = 1; c = 'g'; } else if (source == hairButton) { index = 2; c = 'h'; } else if (source == teethButton) { index = 3; c = 't'; } if (e.getStateChange() == ItemEvent.DESELECTED){ c = '-'; choices.setCharAt(index, c); pictureLabel.setIcon(new ImageIcon("images/geek/geek-" + choices.toString() + ".gif")); pictureLabel.setToolTipText(choices.toString()); } }
public static void main(String s[]) { JFrame frame = new JFrame("CheckBoxDemo"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setContentPane(new CheckBoxDemo()); frame.pack(); frame.setVisible(true); } } Thanks in advance, Angela
John Wetherbie
Rancher
Joined: Apr 05, 2000
Posts: 1441
posted
0
If you could be a little more specific about what is, or is not, happening it would help in troubleshooting. A quick scan didn't cause anything to leap out at me so I probably need to be pointed in the right direction. I'm a little rusty on my AWT/Swing but do you need to call pack and setVisible/show in the CheckBoxDemo class code to actually have it display? John
The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Kathy Rogers
Ranch Hand
Joined: Aug 04, 2000
Posts: 103
posted
0
At the moment, your main method is in the inner class CheckBoxListener because the brackets don't quite match up. Trying putting in another } before the main method. Hope this helps! Kathy
Surya Bahadur
Ranch Hand
Joined: Sep 28, 2000
Posts: 88
posted
0
Hi I have changed the code at some places and i am pasting it here,compare for yourself and see where you have made mistakes
Surya [This message has been edited by Surya Bahadur (edited November 21, 2000).]