aspose file tools
The moose likes Beginning Java and the fly likes What is wrong in this code! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "What is wrong in this code!" Watch "What is wrong in this code!" New topic
Author

What is wrong in this code!

Angela
Greenhorn

Joined: Nov 17, 2000
Posts: 9
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
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
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
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is wrong in this code!
 
Similar Threads
How can I change the button's icon when it's disenabled?
i'm into mess :(
Window Size
Swing classes Vs JDK 1.3! APIs, Methods
Missing Symbol Errors