Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Constructors

The Checkbox control has four different constructors you can use. The first one takes no arguments, and constructs a Checkbox initialized to false with no label text. If you use this constructor, you can set the Checkbox’s text by using the setLabel() method at a later point. The following example shows how to create a Checkbox by using a constructor that takes no arguments:

Checkbox cb = new Checkbox();

The second Checkbox constructor takes a single argument, the Checkbox label text. This Checkbox initializes to a false state. The following example shows how to create a Checkbox control with this constructor:

Checkbox cb = new Checkbox( "My Checkbox" );

The third Checkbox constructor takes three arguments. The first argument is the Checkbox label text, the second argument is a Checkbox group object into which this Checkbox will belong, and the third argument is the initial state in this Checkbox:

Checkbox cb = new Checkbox( "My Checkbox", groupObject, true );

The fourth and final Checkbox constructor takes two arguments. The first argument is a string that sets the Checkbox’s text. The second argument determines the Checkbox’s starting initial state. Here’s an example:

Checkbox cb = new Checkbox( "My Checkbox", true );

Checkbox Events

There are two ways of handling Checkbox events. Both of these are similar to the way we handle Button events. The first way is to extend the Checkbox class and handle the events in the extended Checkbox class itself. There are essentially three steps involved in doing this. The first step is to create a class of your own that extends the Checkbox class. Step number two is to add a constructor to your extended Checkbox class in which you call the enableEvents() method. Unlike what we did with the Button class, we won’t specify an ACTION_EVENT_MASK mask, but we will specify an ITEM_EVENT_MASK. The following line shows how to do it:

enableEvents( AWTEvent.ITEM_EVENT_MASK );

The last thing you’ll do is add a processItemEvent() method to your extended class. Inside your processItemEvent() method, you’ll first take care of whatever tasks you want to perform. Then, you’ll call the super.processItemEvent() method. The following simple example shows the method you need to add:

public void processItemEvent( ItemEvent ie )
{
    // Perform your tasks here…
    super.processItemEvent( ie );
}

The second option for handling Checkbox events is to attach one or more ItemListeners to the Checkbox. To do that, create a new class that implements the ItemListener interface. Put the task to be taken in the itemStateChanged() method. Then associate the newly created class with a check box by using the addItemListener() method. The following source code shows how to create your own class that implements the ItemListener interface. It has the itemStateChanged() method added.

public class MyListener implements ItemListener
{
    public void itemStateChanged( ItemEvent ie )
    {
        if( ie.getSource() == m_Checkbox )
        {
            // Perform tasks here for the checkbox that was selected.
        }
    }
}

The next example shows how to add the ItemListener to your Checkbox control:

m_Checkbox.addItemListener( new MyListener() );

Many times your program will use check boxes and you won’t want to handle the events. As a matter of fact, most of the time you won’t want to handle the events. All you’ll want to do is use the Checkbox getState() method to find out how the Checkbox is set, and use that information to perform whatever actions your program needs to perform.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.