User Tools

Site Tools


swing:jcheckbox

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
swing:jcheckbox [2018/03/23 11:29] – created gthanosswing:jcheckbox [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 14: Line 14:
  * geek-cg--.gif, ..., geek-cght.gif.  * geek-cg--.gif, ..., geek-cght.gif.
  */  */
-public class CheckBoxDemo extends JPanel +public class CheckBoxDemo extends JPanel { 
-              implements ItemListener {+              
   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 checkPanel = new JPanel(new GridLayout(0, 1)); +    JPanel checkBoxPanel = new JPanel(new GridLayout(0, 1)); 
-    checkPanel.add(chinButton); +    checkBoxPanel.add(chinButton); 
-    checkPanel.add(glassesButton); +    checkBoxPanel.add(glassesButton); 
-    checkPanel.add(hairButton); +    checkBoxPanel.add(hairButton); 
-    checkPanel.add(teethButton);+    checkBoxPanel.add(teethButton);
  
-    add(checkPanel, BorderLayout.LINE_START);+    add(checkBoxPanel, BorderLayout.LINE_START);
     add(pictureLabel, BorderLayout.CENTER);     add(pictureLabel, BorderLayout.CENTER);
     setBorder(BorderFactory.createEmptyBorder(20,20,20,20));     setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
   }   }
 +  
 +  ItemListener itemListener = new ItemListener() {
  
-  /** Listens to the check boxes. */ +    /* Listens to the check boxes. */ 
-  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();
  
-    if (source == chinButton) { +      if (source == chinButton) { 
-      index = 0; +        index = 0; 
-      c = 'c'; +        c = 'c'; 
-    } else if (source == glassesButton) { +      } else if (source == glassesButton) { 
-      index = 1; +        index = 1; 
-      c = 'g'; +        c = 'g'; 
-    } else if (source == hairButton) { +      } else if (source == hairButton) { 
-      index = 2; +        index = 2; 
-      c = 'h'; +        c = 'h'; 
-    } else if (source == teethButton) { +      } else if (source == teethButton) { 
-      index = 3; +        index = 3; 
-      c = 't'; +        c = 't'; 
-    }+      }
  
-    //Now that we know which button was pushed, find out +      //Now that we know which button was pushed, find out 
-    //whether it was selected or deselected. +      //whether it was selected or deselected. 
-    if (e.getStateChange() == ItemEvent.DESELECTED) { +      if (e.getStateChange() == ItemEvent.DESELECTED) { 
-      c = '-'; +        c = '-'; 
-    }+      }
  
-    //Apply the change to the string. +      //Apply the change to the string. 
-    choices.setCharAt(index, c);+      choices.setCharAt(index, c);
  
-    updatePicture(); +      updatePicture(); 
-  }+    } 
 +  };
  
   protected void updatePicture() {   protected void updatePicture() {
swing/jcheckbox.1521804595.txt.gz · Last modified: 2018/03/23 11:29 (external edit)