| The demo applets here will illustrate four more of the Swing JComponent 
              classes that allow the user to select among a group of choices or 
              to set values: Each example uses the UiTestApplet 
              class, which adds one of four types of JPanel 
              subclasses. Each subclass has one of the above components and is 
              used to set the background color of another blank panel (OutputPanel) 
              in the top area of the applet. The panels are selected according 
              to the applet tag param 
              value.  For example, the first check box case uses  
              <applet code=UiTestApplet.class 
                width="200" height="80"> <param name="Choose Panel" value="Checkboxes">
 </applet>
  are added to the applet's content pane. JCheckBox 
               Checkboxes are useful for providing several non-exclusive options:  
              
                 
                  | CheckBoxesPanel + UiTestApplet 
                    + OutputPanel
 
 
 |   
                  | import 
                      javax.swing.*;import java.awt.*;
 import java.awt.event.*;
 
 /**
 * Display one of four types of component panels
 * to control the background color on a separate 
                      panel.
 * Applet parameter selects the type of panel.
 **/
 public class UiTestApplet extends JApplet
 {
 /** Create the user interface with OutputPanel 
                      and ControlPanel.**/
 public void init () {
 //Container content_pane = getContentPane 
                      ();
 
 // Create an instance of OutputPanel
 OutputPanel output_panel = new OutputPanel 
                      ();
 
 // Find out from the applet tag 
                      which of the four
 // panels to use. Each panel demonstrates 
                      a different
 // type of control component.
 String panel_choice = getParameter 
                      ("Choose Panel");
 JPanel control_panel;
 
 // Create an instance of the control 
                      panel
 if (panel_choice.equals ("Checkboxes"))
 control_panel 
                      = new CheckBoxesPanel (output_panel);
 else if (panel_choice.equals ("Radiobuttons"))
 control_panel 
                      = new RadioButtonsPanel (output_panel);
 else if (panel_choice.equals ("List"))
 control_panel 
                      = new ListPanel (output_panel);
 else
 control_panel 
                      = new SliderPanel (output_panel);
 
 // Add the panels to applet's pane.
 content_pane.add (output_panel, 
                      BorderLayout.CENTER);
 content_pane.add (control_panel, 
                      BorderLayout.SOUTH);
 } // init
 
 } // class UiTestApplet
 
 |   
                  | import 
                    javax.swing.*; import java.awt.*;
 
 /** Create a JPanel subclass called OutputPanel.
 * It provides a method to set its color plus
 * a paintComponent method.
 **/
 public class OutputPanel extends JPanel {
 Color fBgColor = Color.RED;
 
 /** Set the color by passing the 3 RGB component
 * values  (with ranges 0-255).
 **/
 void setColor (int cr, int cg, int cb) {
 fBgColor = new Color (cr,cg,cb);
 repaint ();
 }
 
 /** For Swing components you must override
 * paintComponent () rather than paint 
                    ().
 **/
 public void paintComponent (Graphics g) {
 super.paintComponent (g);
 
 // Now we fill a rectangle the size 
                    of the
 // panel with the chosen color.
 g.setColor (fBgColor);
 g.fillRect (0, 0, getWidth (), getHeight 
                    () );
 } // paintComponent
 
 } // class OutputPanel
 |   
                  | import 
                    javax.swing.*; import java.awt.*;
 import java.awt.event.*;
 
 /**
 * The CheckBoxesPanel uses check boxes to set
 * the color of the output panel.
 **/
 public class CheckBoxesPanel extends JPanel
 implements ItemListener
 {
 JCheckBox fRed, fGreen, fBlue;
 OutputPanel fOutputPanel;
 
 /** Constructor adds 3 check boxes to the panel. 
                    **/
 CheckBoxesPanel (OutputPanel output_panel) {
 
 fOutputPanel = output_panel;
 
 // Initial color is red so select 
                    this button.
 fRed = new JCheckBox ("Red", true);
 fRed.addItemListener (this);
 add (fRed);
 
 fGreen = new JCheckBox ("Green");
 fGreen.addItemListener (this);
 add (fGreen);
 
 fBlue = new JCheckBox ("Blue");
 fBlue.addItemListener (this);
 add (fBlue);
 
 }// ctor
 
 /**
 * An item event from a check box comes 
                    here when
 * the user clicks on one.
 **/
 public void itemStateChanged (ItemEvent evt) {
 if (evt.getStateChange () == ItemEvent.SELECTED)
 System.out.println 
                    ("SELECTED");
 else
 System.out.println 
                    ("DESELECTED");
 int cr = (fRed.isSelected ()   
                    ? 0xFF : 0);
 int cg = (fGreen.isSelected () ? 0xFF 
                    : 0);
 int cb = (fBlue.isSelected ()  ? 
                    0xFF : 0);
 
 fOutputPanel.setColor (cr,cg,cb);
 
 } // itemStateChanged
 
 } // class CheckBoxesPanel
 |  JRadioButton Radio buttons are for those cases where only one 
                of several options can be selected:  
                
                   
                    | RadioButtonsPanel + UITestApplet 
                      + ControlPanel
 
 |   
                    | import 
                        javax.swing.*;import java.awt.*;
 import java.awt.event.*;
 import javax.swing.event.*;
 
 /**
 * The RadioButtonsPanel holds radio buttons 
                        that set
 * the color of the output panel.
 **/
 public class RadioButtonsPanel extends JPanel
 implements ActionListener
 {
 
 JRadioButton fRed = null, fGreen = null, fBlue 
                        = null;
 OutputPanel fOutputPanel = null;
 
 
 RadioButtonsPanel (OutputPanel output_panel) 
                        {
 
 fOutputPanel = output_panel;
 
 // RadioButtons need to be organized 
                        with a
 // ButtonGroup object.
 ButtonGroup group = new ButtonGroup 
                        ();
 
 fRed = new JRadioButton ("Red", 
                        true);
 fRed.addActionListener (this);
 
 // Add the JRadioButton instance 
                        to both the
 // ButtonGroup and the panel.
 group.add (fRed);
 add (fRed);
 
 fGreen = new JRadioButton ("Green", 
                        false);
 fGreen.addActionListener (this);
 group.add (fGreen);
 add (fGreen);
 
 fBlue = new JRadioButton ("Blue", 
                        false);
 fBlue.addActionListener (this);
 group.add (fBlue);
 add (fBlue);
 
 } // ctor
 
 /** Action events come here when the user 
                        clicks on a button.**/
 public void actionPerformed (ActionEvent evt)  {
 
 // Default color component values.
 int cr=0;
 int cg=0;
 int cb=0;
 // Only one button can be selected 
                        at a time
 // so choose one color and then 
                        set the output
 // panel color.
 Object source = evt.getSource 
                        ();
 if ( source == fRed)
 cr = 0xFF;
 else if (source == fGreen)
 cg = 0xFF;
 else if (source == fBlue)
 cb = 0xFF;
 
 fOutputPanel.setColor (cr,cg,cb);
 } // actionPerformed
 
 } // class RadioButtonsPanel
 |    JList The JList 
                component provides a way of offering a list of items from which 
                the user can select.  Depending on how the JList 
                instance is configured, either a single item can be selected, 
                or multiple items selected.   
                
                   
                    | ListPanel + UITestApplet 
                      + OutputPanel
 
 
 |   
                    | import 
                      javax.swing.*; import java.awt.*;
 import java.awt.event.*;
 import javax.swing.event.*;
 
 /**
 * The ListPanel holds the JList component with 
                      items
 * that determine the color of the output panel.
 **/
 public class ListPanel extends JPanel
 implements 
                      ListSelectionListener
 {
 
 OutputPanel fOutputPanel;
 
 /** Constructor adds a JList to the panel. **/
 ListPanel (OutputPanel output_panel) {
 
 fOutputPanel = output_panel;
 
 String [] colors = {
 "Red","Green","Blue","Yellow","White","Black"};
 
 JList color_list = new JList (colors);
 
 // Show only 4 items in the list 
                      at a time.
 color_list.setVisibleRowCount (4);
 
 // Allow only one of the items to 
                      be selected.
 color_list.setSelectionMode (
 ListSelectionModel.SINGLE_SELECTION);
 
 // Select initially the top item.
 color_list.setSelectedIndex (0);
 
 color_list.addListSelectionListener 
                      (this);
 
 // Add to a JScrollPane so that 
                      we can have
 // a scroller to view other items.
 JScrollPane scroll_pane = new JScrollPane 
                      (color_list);
 add (scroll_pane);
 
 } // ctor
 
 /** This class implements the ListSelectionListener
 * so events come to this valueChanged 
                      method.
 **/
 public void valueChanged (ListSelectionEvent 
                      evt)  {
 
 // Default color component values.
 int cr=0;
 int cg=0;
 int cb=0;
 
 // Get the reference to the JList 
                      object
 JList source =  (JList)evt.getSource 
                      ();
 // and get an array of the selected 
                      items.
 Object [] values = source.getSelectedValues 
                      ();
 
 // In this case only one value can 
                      be selected
 // so just look at first item in 
                      array.
 String color_selected =  (String)values[0];
 if ( color_selected.equals ("Red") 
                      )
 cr = 0xFF;
 else if (color_selected.equals ("Green") 
                      )
 cg = 0xFF;
 else if (color_selected.equals ("Blue") 
                      )
 cb = 0xFF;
 else if (color_selected.equals ("Yellow") 
                      ){
 cr = 0xFF;
 cg = 0xFF;
 }else if (color_selected.equals 
                      ("White") ){
 cr = 0xFF;
 cg = 0xFF;
 cb = 0xFF;
 }
 
 fOutputPanel.setColor (cr,cg,cb);
 
 } // valueChanged
 
 } // class ListPanel
 |    JSlider Sliders allow the user to choose from a continuous 
                  range of values rather than from discrete values in a set of 
                  radio buttons or in a list.  
                
                   
                    | SliderPanel + UITestApplet 
                      + OutputPanel
 
 |   
                    | import 
                      java.awt.*; import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.event.*;
 
 /**
 * The SliderPanel holds three JSlider widgets 
                      that set
 * the color of the output panel.
 **/
 public class SliderPanel extends JPanel
 implements ChangeListener {
 
 OutputPanel fOutputPanel;
 JLabel fRedLabel,fGreenLabel,fBlueLabel;
 JSlider fRed,fGreen,fBlue;
 
 SliderPanel (OutputPanel output_panel) {
 
 fOutputPanel = output_panel;
 
 setLayout (new GridLayout (3, 2));
 
 add (fRedLabel = new JLabel ("Red 
                      0  ",SwingConstants.RIGHT) );
 
 // The JSlider constructor parameters:
 //
 //   orientation, minimum, 
                      maximum, inital value
 //
 // The sliders are set horizontally.
 // The values range from 0 to 255.
 // Set the red slider to max initially 
                      to match the
 // initial Red color for the output 
                      panel.
 //
 add (fRed = new JSlider (JSlider.HORIZONTAL, 
                      0, 255, 255));
 fRed.addChangeListener (this);
 
 add (fGreenLabel = new JLabel ("Green 
                      0  ",SwingConstants.RIGHT));
 add (fGreen = new JSlider (Adjustable.HORIZONTAL, 
                      0, 255, 0));
 fGreen.addChangeListener (this);
 
 add (fBlueLabel = new JLabel ("Blue 
                      0  ",SwingConstants.RIGHT) );
 add (fBlue = new JSlider (Adjustable.HORIZONTAL, 
                      0, 255, 0));
 fBlue.addChangeListener (this);
 
 } // ctor
 
 /** This class is the AdjustmentListener for 
                      the
 * scroll bar. So the events come 
                      here when the
 * scroll bar is moved.
 **/
 public void stateChanged (ChangeEvent evt){
 
 // Use the labels to show the numerical 
                      values of the
 // scroll bar settings.
 fRedLabel.setText (  "Red 
                      "   + fRed.getValue ()  );
 fGreenLabel.setText ("Green " + 
                      fGreen.getValue ());
 fBlueLabel.setText ( "Blue "  + 
                      fBlue.getValue () );
 
 // Get the values from each scroll 
                      bar and pass
 // them as the color component values.
 fOutputPanel.setColor (fRed.getValue 
                      (), fGreen.getValue (),
 fBlue.getValue ());
 
 fOutputPanel.repaint ();
 
 } // stateChanged
 
 } // class SliderPanel
 |  The above examples give the essential steps for 
                  using these components. However, they provide many other options. 
                  For more information on the above components, see the API 
                  Specifications and the resources below. References and Web 
                  Resources  Latest update: Nov. 3, 2006 |