Monday 27 March 2017

Applying Check Boxes, CheckboxGroup & Choice Controls - Java Tutorials

Applying Check Boxes

A check box is a control that is used to turn an option on or off. It consists of a small box that can either contain a check mark or not. There is a label associated with each check box that describes what option the box represents. You change the state of a check box by clicking on it. Check boxes can be used individually or as part of a group. Check boxes are objects of the Checkbox class.

Checkbox supports these constructors:

      Checkbox( )
      Checkbox(String str)
      Checkbox(String str, boolean on)
      Checkbox(String str, boolean on, CheckboxGroup cbGroup)
      Checkbox(String str, CheckboxGroup cbGroup, boolean on)

The first form creates a check box whose label is initially blank. The state of the check box is unchecked. The second form creates a check box whose label is specified by str. The state of the check box is unchecked. The third form allows you to set the initial state of the check box. If on is true, the check box is initially checked; otherwise, it is cleared. The fourth and fifth forms create a check box whose label is specified by str and whose group is specified by cbGroup. If this check box is not part of a group, then cbGroup must be null. (Check box groups are described in the next section.) The value of on determines the initial state of the check box.

To retrieve the current state of a check box, call getState( ). To set its state, call setState( ). You can obtain the current label associated with a check box by calling getLabel( ). To set the label, call setLabel( ). These methods are as follows:

      boolean getState( )
      void setState(boolean on)
      String getLabel( )
      void setLabel(String str)

Here, if on is true, the box is checked. If it is false, the box is cleared. The string passed in str becomes the new label associated with the invoking check box.


Handling Check Boxes

Each time a check box is selected or deselected, an item event is generated. This is sent to any listeners that previously registered an interest in receiving item event notifications from that component. Each listener implements the ItemListener interface. That interface defines the itemStateChanged( ) method. An ItemEvent object is supplied as the argument to this method. It contains information about the event (for example, whether it was a selection or deselection).

The following program creates four check boxes. The initial state of the first box is checked. The status of each check box is displayed. Each time you change the state of a check box, the status display is updated.

  // Demonstrate check boxes.
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
  /*
    <applet code="CheckboxDemo" width=250 height=200>
    </applet>
  */
  public class CheckboxDemo extends Applet implements ItemListener {
    String msg = "";
    Checkbox Win98, winNT, solaris, mac;

    public void init() {
      Win98 = new Checkbox("Windows 98/XP", null, true);
      winNT = new Checkbox("Windows NT/2000");
      solaris = new Checkbox("Solaris");
      mac = new Checkbox("MacOS");

      add(Win98);
      add(winNT);
      add(solaris);
      add(mac);

      Win98.addItemListener(this);
      winNT.addItemListener(this);
      solaris.addItemListener(this);
      mac.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent ie) {
      repaint();
    }

    // Display current state of the check boxes.
    public void paint(Graphics g) {
      msg = "Current state: ";
      g.drawString(msg, 6, 80);
      msg = " Windows 98/XP: " + Win98.getState();
      g.drawString(msg, 6, 100);
      msg = " Windows NT/2000: " + winNT.getState();
      g.drawString(msg, 6, 120);
      msg = " Solaris: " + solaris.getState();
      g.drawString(msg, 6, 140);
      msg = " MacOS: " + mac.getState();
      g.drawString(msg, 6, 160);
    }
  }




CheckboxGroup

It is possible to create a set of mutually exclusive check boxes in which one and only one check box in the group can be checked at any one time. These check boxes are often called radio buttons, because they act like the station selector on a car radio—only one station can be selected at any one time. To create a set of mutually exclusive check boxes, you must first define the group to which they will belong and then specify that group when you construct the check boxes. Check box groups are objects of type CheckboxGroup. Only the default constructor is defined, which creates an empty group.

You can determine which check box in a group is currently selected by calling getSelectedCheckbox  ( ). You can set a check box by calling setSelectedCheckbox( ). These methods are as follows:

      Checkbox getSelectedCheckbox( )
      void setSelectedCheckbox(Checkbox which)

Here, which is the check box that you want to be selected. The previously selected check box will be turned off. Here is a program that uses check boxes that are part of a group:

  // Demonstrate check box group.
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
  /*
    <applet code="CBGroup" width=250 height=200>
    </applet>
  */

  public class CBGroup extends Applet implements ItemListener {
    String msg = "";
    Checkbox Win98, winNT, solaris, mac;
    CheckboxGroup cbg;

    public void init() {
      cbg = new CheckboxGroup();
      Win98 = new Checkbox("Windows 98/XP", cbg, true);
      winNT = new Checkbox("Windows NT/2000", cbg, false);
      solaris = new Checkbox("Solaris", cbg, false);
      mac = new Checkbox("MacOS", cbg, false);

      add(Win98);
      add(winNT);
      add(solaris);
      add(mac);

      Win98.addItemListener(this);
      winNT.addItemListener(this);
      solaris.addItemListener(this);
      mac.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent ie) {
      repaint();
    }

    // Display current state of the check boxes.
    public void paint(Graphics g) {
      msg = "Current selection: ";
      msg += cbg.getSelectedCheckbox().getLabel();
      g.drawString(msg, 6, 100);
    }
  }




Choice Controls

The Choice class is used to create a pop-up list of items from which the user ma choose. Thus, a Choice control is a form of menu. When inactive, a Choice component takes up only enough space to show the currently selected item. When the user clicks on it, the whole list of choices pops up, and a new selection can be made. Each item in the list is a string that appears as a left-justified label in the order it is added to the Choice object. Choice only defines the default constructor, which creates an empty list. To add a selection to the list, call add( ). It has this general form:

      void add(String name)

Here, name is the name of the item being added. Items are added to the list in the order in which calls to add( ) occur. To determine which item is currently selected, you may call either getSelectedItem( ) or getSelectedIndex( ). These methods are shown here:

      String getSelectedItem( )
      int getSelectedIndex( )

The getSelectedItem( ) method returns a string containing the name of the item. getSelectedIndex( ) returns the index of the item. The first item is at index 0. By default, the first item added to the list is selected.

To obtain the number of items in the list, call getItemCount( ). You can set the currently selected item using the select( ) method with either a zero-based integer index or a string that will match a name in the list. These methods are shown here:

      int getItemCount( )
      void select(int index)
      void select(String name)

Given an index, you can obtain the name associated with the item at that index by calling getItem( ), which has this general form:

      String getItem(int index)

Here, index specifies the index of the desired item.


Handling Choice Lists

Each time a choice is selected, an item event is generated. This is sent to any listeners that previously registered an interest in receiving item event notifications from that component. Each listener implements the ItemListener interface. That interface defines the itemStateChanged( ) method. An ItemEvent object is supplied as the argument to this method.

Here is an example that creates two Choice menus. One selects the operating system. The other selects the browser.

  // Demonstrate Choice lists.
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
  /*
    <applet code="ChoiceDemo" width=300 height=180>
    </applet>
  */

  public class ChoiceDemo extends Applet implements ItemListener {
    Choice os, browser;
    String msg = "";

    public void init() {
      os = new Choice();
      browser = new Choice();

      // add items to os list
      os.add("Windows 98/XP");
      os.add("Windows NT/2000");
      os.add("Solaris");
      os.add("MacOS");

      // add items to browser list
      browser.add("Netscape 3.x");
      browser.add("Netscape 4.x");
      browser.add("Netscape 5.x");
      browser.add("Netscape 6.x");

      browser.add("Internet Explorer 4.0");
      browser.add("Internet Explorer 5.0");
      browser.add("Internet Explorer 6.0");
      
      browser.add("Lynx 2.4");

      browser.select("Netscape 4.x");

      // add choice lists to window
      add(os);
      add(browser);

      // register to receive item events
      os.addItemListener(this);
      browser.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent ie) {
      repaint();
    }

    // Display current selections.
    public void paint(Graphics g) {
      msg = "Current OS: ";
      msg += os.getSelectedItem();
      g.drawString(msg, 6, 120);
      msg = "Current Browser: ";
      msg += browser.getSelectedItem();
      g.drawString(msg, 6, 140);
    }
  }

No comments:

Post a Comment