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


When the program runs, you’ll see three buttons at the top of the window. There’s a text string below them that tells you which button has been pressed. When the program first runs, it tells you that no buttons have been pressed, but by pressing any of the buttons, you cause the text string in the window to change. Figure 7.1 shows the program running within Internet Explorer.


Figure 7.1  The first ButtonDemo program.

The extended Button class is named MyButton. Its declaration begins at line 25. The constructor just takes a string like the normal Button constructor. The first thing that happens inside of a constructor is that it calls the super() method. The next thing it does is call the enableEvents() method. As I said earlier, you must call this method in order for the extended Button class to handle events. The last thing that the Button constructor does is store the button text so that we can use it later when we display which button was pressed.

At line 36, the processActionEvent() method begins. It’s this method that handles the events. The first thing that happens in the method is that it sets the display string so that the next time the applet window is repainted, you’ll see which button was pressed. The next thing it does is to cause its parent, which is the Main applet window, to repaint. The last thing it does is call the super.processActionEvent() method.

Now, notice back at lines 9, 10, and 11 that instead of creating normal button objects, we’re creating MyButton objects. We do this because we need to use the extended Button class to handle the events.

There’s another way of handling the Button events, as mentioned earlier. I’ve written a second program, shown in Listing 7.2, that has an identical appearance to the first program but implements the Button event handling differently. Why would you need two different ways to handle Button events? Well, it depends on the program you’re writing and what’s easier for you. In certain circumstances one way will be easier, and in other circumstances the other way might be more to your liking.

Listing 7.2 This ButtonDemo Program Shows How to Implement the actionListener Interface

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      Button m_Button1 = new Button( "First Button" );
10      Button m_Button2 = new Button( "Second Button" );
11      Button m_Button3 = new Button( "Third Button" );
12
13      MyListener Listener = new MyListener();
14
15      public void init()
16      {
17          m_Button1.addActionListener( Listener );
18          m_Button2.addActionListener( Listener );
19          m_Button3.addActionListener( Listener );
20          add( m_Button1 );
21          add( m_Button2 );
22          add( m_Button3 );
23      }
24
25      public void paint( Graphics g )
26      {
27          g.drawString( m_strDisplay, 20, 120 );
28      }
29
30      public class MyListener implements ActionListener
31      {
32          public void actionPerformed( ActionEvent ae )
33          {
34              if( ae.getSource() == m_Button1 )
35              {
36                  m_strDisplay = "The 'First Button' was pressed.";
37                  repaint();
38              }
39              else if( ae.getSource() == m_Button2 )
40              {
41                  m_strDisplay = "The 'Second Button' was pressed.";
42                  repaint();
43              }
44              else if( ae.getSource() == m_Button3 )
45              {
46                  m_strDisplay = "The 'Third Button' was pressed.";
47                  repaint();
48              }
49          }
50      }
51
52  }

This program will look exactly like the first ButtonDemo program. When it first runs, you’ll see that no button has been pressed, but each time you press a button, the text will change and you’ll see which button was pressed.

The first thing we’ll look at in this sample program is line 30. We’ve created a class that implements the ActionListener interface. In line 32, you can see that we’ve added an actionPerformed() method. It will be to this method that all the Button events will go. Inside the actionPerformed() method, we used the Action event getSource() method to find out which button was pressed. After determining which button was pressed, we change the display string text, and then we repaint the applet’s window so that users see which button was pressed. Notice that in lines 9–11, we create standard Button objects. In line 13, notice that we create a MyListener class. Remember that the MyListener class is our newly created class that implements the ActionListener interface. We’re going to use this MyListener class for each of the buttons. Look at lines 17, 18, and 19. In these lines, we’re telling the Button objects to use the ActionListener we created. After we’ve done this, all the Button actions will go to the MyListener class.

The Checkbox Control

The Checkbox user interface component has two states: true (or selected) and false (or deselected). Checkbox components trigger user events when selected and deselected. When more than one Checkbox appears within a container, they can either be independently selected or be exclusive of each other. For Checkbox controls to be exclusive of each other, they must all belong to the same group. Only one Checkbox component in a group can be selected at a time. The Checkbox components in a group are usually referred to as radio buttons. In Java, you can give Checkbox components the behavior of radio buttons by associating each component with another object by Checkbox group. We’ll talk about the Checkbox group later in the next section.


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.