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


Events

Mouse and keyboard events are generated for labels, but no selection or action events are. It’s rare that you’ll need to handle events from labels. Java 1.0 did not even have this capability.

Sample Program

This section just presents a simple program that displays four labels, all with different alignments. The source code is given in Listing 7.11.

Listing 7.11 This Simple Program Shows Simple Label Alignments

1   import java.awt.*;
2   import java.applet.*;
3
4   public class Applet1 extends Applet
5   {
6
7       public void init()
8       {
9           Label label1, label2, label3, label4;
10          label1 = new Label( "        Label 1        " );
11          label2 = new Label( "        Label 2        ", Label.LEFT );
12          label3 = new Label( "        Label 3        ", Label.RIGHT );
13          label4 = new Label( "        Label 4        ", Label.CENTER );
14
15          Font font = new Font( "Helvetica", Font.BOLD, 25 );
16          label2.setFont( font );
17          label3.setFont( font );
18          label4.setFont( font );
19
20          add( label1 );
21          add( label2 );
22          add( label3 );
23          add( label4 );
24
25      }
26
27  }

When the program runs, you’ll see four labels in the applet window, as shown in Figure 7.7. The first one uses the default text and the default alignment. The next three use a larger font with a left alignment, right alignment, and center alignment.


Figure 7.7  This program displays text in Label controls with different alignments.

Summary

Java provides the functionality required to implement cross-platform Java applets with sophisticated and interactive user interfaces. The Advanced Windowing Toolkit contains many interface components. These can be placed within containers via the add() method. A great deal of your Java programming will contain and include these Java interface components.

Q&A

Q How many ways are there to handle Button events?

A There are two ways. One approach is to extend the Button class, call the enableEvents() method, and add a processActionEvent() method to the class for handling the events. Another approach is to create a class that implements the ActionListener interface. Using the addActionListener() method to add an instantiation of the newly created class that implements the ActionListener interface will then cause the new class’s actionPerformed() method to get called whenever an action event is triggered.

Q How can you use check boxes without handling Checkbox events?

A Anytime you want to get the state of a check box, simply call the getState() method. If it returns true, the check box is selected; false, the check box is not selected.

Q How many constructors does the Choice control have?

A One. The Choice control’s only constructor takes no arguments. To add selectable items to a Choice control, you have to use the addItem() method.

Q How do you make check boxes act like radio buttons such that only one simultaneous selection in the group is allowed?

A You must first create a CheckboxGroup control. Then when you create the Checkbox controls, use the constructor that accepts a CheckboxGroup object as an argument. All the check boxes that are placed into this CheckboxGroup will be grouped together in a radio-button family.

Q What’s the different between an Edit control and a TextArea control?

A An Edit control contains a single line of editable text. A TextArea control contains multiple lines of text.

Q When would you want to not handle events for user-interface controls?

A When you have no action that must be performed during an event. A good example of this is a Checkbox control. You might only need to know what the user has selected when you go to perform a task. In this case, you can simply use the getState() method to find out whether the check box is selected or deselected. This approach requires far less code than is required to handle Checkbox events. On the other hand, if you need to take some action when the user clicks on a check box, you’ll have to handle the Checkbox events.

Q What happens if you don’t use the add() method to add these controls to the applet’s window?

A Nothing. The controls will not be displayed to users, and they won’t have the ability to interact with the control.

Q Can I write my own custom controls and use them rather than the controls discussed in this chapter?

A Sure, if you want to work harder than you have to and not provide cross-platform look and feel. First of all, it’s hard to write a control. Second, the beauty of these controls is that they fit the platform on which they’re running.

Exercises

1.  Create an applet and create a class that extends the Button class. In the constructor, call the enableEvents() method with the AWTEvent.ACTION_EVENT_MASK argument. Add a processActionEvent() method to your extended class. Then add a label at the top of the applet into which you’ll alert users to events that your extended class handled. Add three buttons to the applet, but create them from your extended Button class. In the processActionEvent() method, set the label text so that users know what event you just handled. Compile and run the program.
2.  Create an applet and create a class that extends the Checkbox class. Make sure that the extended class calls the actionEvent() method with the AWTEvent.ITEM_EVENT_MASK argument, and that the class has a processActionEvent() method. Create a CheckboxGroup. Create three Checkboxes from your extended class. Make sure that you’ve added a constructor that takes a CheckboxGroup object. Then, when you create the Checkboxes from your extended class, use the constructor that lets you give it the CheckboxGroup object. When a Checkbox event is triggered, update a label in the window to show the user what event you just handled.


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.