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 TextField control has four constructors. The first constructor constructs the new text field and takes no arguments. The following source code shows how to create a TextField argument with this constructor:

TextField tf = new TextField();

The second constructor allows you to specify the number of columns for a text field. This is important when you want the text field to be a certain size. The following example shows how to create a TextField object that is 25 columns wide:

TextField tf = new TextField( 25 );

The third TextField constructor takes a single argument: a string that is in the text field when it first appears. The TextField will be created with enough columns to accommodate the text string that’s been passed to the constructor. The following example shows how to create a text field with an initial string of “Hello World”:

TextField tf = new TextField( "Hello World" );

The last constructor takes both a string and the number of columns that specify how wide the text field will be. The following source code creates a text field with an initial string of “Hello World” and a column width of 25:

TextField tf = new TextField( "Hello World", 25 );

As with the other components that we’ve talked about thus far, you have two ways of handling events in a TextField object. In the case of a TextField object, by far the easiest way to extend is to create a new class that extends the TextField class. TextFields trigger four types of events: focus events, keyboard events, text events, and action events. The difference between keyboard events and text events is that text events are generated only when the setTextMethod() is called programmatically. I’ve created an example that shows how to extend the TextField class so that you can catch focus events, keyboard events, and action events. Unlike the other examples, this one is not so simple. I’ve also included only one constructor in my extended class. It’s the constructor that takes a string as an argument. If you want the class that you extend to be flexible, you’ll have to have all four kinds of constructors that the TextField class has. Inside of my extended class, I add a processKeyEvent() method, a processActionEvent() method, and a processFocusEvent() method. Any one of these three methods can be triggered by events that my extended TextField class triggers. The source code for my extended class follows:

public class MyTextField extends TextField
{

    MyTextField( String s )
    {
        super( s );
        enableEvents( AWTEvent.ACTION_EVENT_MASK |
            AWTEvent.FOCUS_EVENT_MASK | AWTEvent.KEY_EVENT_MASK );
    }

    public void processKeyEvent( KeyEvent ke )
    {
        if( ke.getID() == ke.KEY_RELEASED )
        {
            // Respond to keypress here…
        }
        super.processKeyEvent( ke );
    }

    public void processActionEvent( ActionEvent ae )
    {
        // Handle the action event here…
        super.processActionEvent( ae );
    }

    public void processFocusEvent( FocusEvent fe )
    {
        if( fe.getID() == fe.FOCUS_GAINED )
        {
            // Respond to focus here..
        }
        super.processFocusEvent( fe );
    }

}

Sample Program

Because we’ve really focused only on one method of handling TextField events, we’re going to limit ourselves to one sample program. This sample program extends the TextField class and allows it to handle the events. You can see the source code for this sample program in Listing 7.10.

Listing 7.10 This Program Uses a Class That Extends the TextField Class

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 event.";
8
9       MyTextField tf = new MyTextField( "Hello TextField!" );
10
11      public void init()
12      {
13          add( tf );
14      }
15
16      public void paint( Graphics g )
17      {
18          g.drawString( m_strDisplay, 20, 150 );
19      }
20
21      public class MyTextField extends TextField
22      {
23
24          MyTextField( String s )
25          {
26              super( s );
27              enableEvents( AWTEvent.ACTION_EVENT_MASK |
28                  AWTEvent.FOCUS_EVENT_MASK | AWTEvent.KEY_EVENT_MASK );
29          }
30
31          public void processKeyEvent( KeyEvent ke )
32          {
33              if( ke.getID() == ke.KEY_RELEASED )
34              {
35                  m_strDisplay = "The key released was "
                    ⇒+ ke.getKeyChar() + ".";
36                  getParent().repaint();
37              }
38              super.processKeyEvent( ke );
40
41          public void processActionEvent( ActionEvent ae )
42          {
43              m_strDisplay = "There was an action event.";
44              getParent().repaint();
45              super.processActionEvent( ae );
46          }
47
48          public void processFocusEvent( FocusEvent fe )
49          {
50              if( fe.getID() == fe.FOCUS_GAINED )
51              {
52                  m_strDisplay = "The focus was gained.";
53                  getParent().repaint();
54              }
55              else if( fe.getID() == fe.FOCUS_LOST )
56              {
57                  m_strDisplay = "The focus was lost.";
58                  getParent().repaint();
59              }
60              super.processFocusEvent( fe );
61          }
62
63      }
64
65  }


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.