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


Day 7
Applet User Interface Controls

Today you’ll learn about the user interface controls you can use when you create applets. These controls are all extended from the Component class, so they inherit all the Component class methods. These controls are all windows; but unlike Frame windows in the normal applet window, you won’t be drawing to these windows. Instead, these controls have their own graphical representation that adopts the look and feel of the operating system in which they’re running. On one hand, this approach saves you from having to adapt to multiple platforms and manage the details of implementing controls on the various platforms. On the other hand, you have less control on these graphical objects.

Today, you will learn about the following topics:

  The Button control
  The Checkbox control
  The Checkbox group control
  The Choice control
  The ListBox control
  The TextField control
  The TextArea control
  The Label control

After you create a component, you add it to a window. After you’ve added a control to a window, you’ll want to interact with it. There are several ways of doing this, and we’ll talk about all of them in this chapter.

The Button Control

A button is one of the most common user interface components. A button can be constructed with a text string, and it has the capability to trigger a user event when it’s pressed.

Constructors

You can create button objects in two ways. The first method uses the Button constructor that takes a text string. The following example shows how to do this:

Button Button1 = new Button( "Button Text" );

The second way to create a button object is by simply calling the Button constructor that takes no argument. Later, you’ll have to use the Button class’s setLabel() method to give the button some sort of text. I’ve created a simple example that shows how:

Button Button2 = new Button();
Button2.setLabel( "Button Text" );

Handling Events

You can handle button events in two ways. The first method is by extending the Button class, enabling events in the extended Button class, and overriding the processActionEvent() method to perform the tasks you want. The following line of source code shows how to enable events in your Button class. I usually call this method from my extended Button class’s constructor.

enableEvents( AWTEvent.ACTION_EVENT_MASK );

The next thing you need to do is to add a processActionEvent() method to your extended Button class. The first thing you’ll do inside the processActionEvent() method is to perform the many tasks you’ll need to take care of; the last thing you’ll do in this method is to call the super.processActionEvent() method. The following example shows what you’d add to your extended Button class to handle Action events:

public void processActionEvent( ActionEvent ae )
{
    // Perform your tasks here…
    super.processActionEvent( ae );
}

The second way of handling Button events is to attach one or more ActionListeners to the button. To do that, create an object that implements the ActionListener interface. Put the action to be taken in the actionPerformed() method, and then associate it with a button using the addActionListener() method. The following example shows how to create a class that implements the ActionListener interface:

public class MyListener implements ActionListener
{
    public void actionPerformed( ActionEvent ae )
    {
        if( ae.getSource() == Button1 )
        {
            // Perform task here for Button1…
        }
    }
}

After you’ve added the class that implements the ActionListener, for each button that you want to handle ActionEvents, you need to use its addActionListener() method. The following example shows how to do this:

Button1.addActionListener( new MyListener() );

I created a program, shown in Listing 7.1, that adds three buttons to the applet window. I then created a class in the applet that extends the Button class. I enabled events and then added a processActionEvent() method. So the way I’m handling the events in this sample program is the same as the first approach to handling events that I described earlier.

Listing 7.1 The ButtonDemo Program Shows an Extended Button Class That Handles the Events

1   import java.awt.*;
2   import java.applet.*;
3   import java.awt.event.*;
4
5   public class Applet1 extends Applet
6   {
7       String m_strDisplay = "No buttons have been pressed.";
8
9       MyButton m_Button1 = new MyButton( "First Button" );
10      MyButton m_Button2 = new MyButton( "Second Button" );
11      MyButton m_Button3 = new MyButton( "Third Button" );
12
13      public void init()
14      {
15          add( m_Button1 );
16          add( m_Button2 );
17          add( m_Button3 );
18      }
19
20      public void paint( Graphics g )
21      {
22          g.drawString( m_strDisplay, 20, 120 );
23      }
24
25      public class MyButton extends Button
26      {
27          String m_strText;
28
29          MyButton( String s )
30          {
31              super( s );
32              enableEvents( AWTEvent.ACTION_EVENT_MASK );
33              m_strText = s;
34          }
35
36          public void processActionEvent( ActionEvent ae )
37          {
38              m_strDisplay = "The '" + m_strText + "' was pressed";
39              getParent().repaint();
40              super.processActionEvent( ae );
41          }
42      }
43
44  }


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.