swing:jcheckbox
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
swing:jcheckbox [2018/03/23 11:29] – created gthanos | swing:jcheckbox [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 14: | Line 14: | ||
* geek-cg--.gif, | * geek-cg--.gif, | ||
*/ | */ | ||
- | public class CheckBoxDemo extends JPanel | + | public class CheckBoxDemo extends JPanel |
- | | + | |
JCheckBox chinButton; | JCheckBox chinButton; | ||
JCheckBox glassesButton; | JCheckBox glassesButton; | ||
Line 45: | Line 45: | ||
//Register a listener for the check boxes. | //Register a listener for the check boxes. | ||
- | chinButton.addItemListener(this); | + | chinButton.addItemListener(itemListener); |
- | glassesButton.addItemListener(this); | + | glassesButton.addItemListener(itemListener); |
- | hairButton.addItemListener(this); | + | hairButton.addItemListener(itemListener); |
- | teethButton.addItemListener(this); | + | teethButton.addItemListener(itemListener); |
//Indicates what's on the geek. | //Indicates what's on the geek. | ||
Line 59: | Line 59: | ||
//Put the check boxes in a column in a panel | //Put the check boxes in a column in a panel | ||
- | JPanel | + | JPanel |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | add(checkPanel, BorderLayout.LINE_START); | + | add(checkBoxPanel, BorderLayout.LINE_START); |
add(pictureLabel, | add(pictureLabel, | ||
setBorder(BorderFactory.createEmptyBorder(20, | setBorder(BorderFactory.createEmptyBorder(20, | ||
} | } | ||
+ | | ||
+ | ItemListener itemListener = new ItemListener() { | ||
- | | + | |
- | public void itemStateChanged(ItemEvent e) { | + | public void itemStateChanged(ItemEvent e) { |
- | int index = 0; | + | int index = 0; |
- | char c = ' | + | char c = ' |
- | Object source = e.getItemSelectable(); | + | Object source = e.getItemSelectable(); |
- | | + | |
- | index = 0; | + | index = 0; |
- | c = ' | + | c = ' |
- | } else if (source == glassesButton) { | + | } else if (source == glassesButton) { |
- | index = 1; | + | index = 1; |
- | c = ' | + | c = ' |
- | } else if (source == hairButton) { | + | } else if (source == hairButton) { |
- | index = 2; | + | index = 2; |
- | c = ' | + | c = ' |
- | } else if (source == teethButton) { | + | } else if (source == teethButton) { |
- | index = 3; | + | index = 3; |
- | c = ' | + | c = ' |
- | } | + | } |
- | | + | |
- | //whether it was selected or deselected. | + | //whether it was selected or deselected. |
- | if (e.getStateChange() == ItemEvent.DESELECTED) { | + | if (e.getStateChange() == ItemEvent.DESELECTED) { |
- | c = ' | + | c = ' |
- | } | + | } |
- | | + | |
- | choices.setCharAt(index, | + | choices.setCharAt(index, |
- | | + | |
- | } | + | } |
+ | }; | ||
protected void updatePicture() { | protected void updatePicture() { |
swing/jcheckbox.1521804595.txt.gz · Last modified: 2018/03/23 11:29 (external edit)