AWT Button Action Handler at the Frame Level

This section provides a tutorial example on how to create an AWT button to handle actions by adding the ActionListener interface to the frame that contains the button component.



In the previous section, we extended the button to handle its own action. That works fine, if the action only requires modifying the behavior of the same button.

But if the action requires modifying the behavior of other components, you need to implement the action listener as a higher level. The following program shows you how to use the action handler to modify the text of a label component:

/* ButtonAction2.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.awt.Frame;
import java.awt.Button;
import java.awt.Label;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonAction2 implements ActionListener {
   Button myButton = null;
   Label myLabel = null;
   String text = null;
   public static void main(String[] a) {
      ButtonAction2 myTest = new ButtonAction2();
      myTest.createFrame();
   }
   public void createFrame() {
      Frame f = new Frame("My Switch Button");
      f.setLayout(new GridLayout(2,1));
      text = "On";
      myButton = new Button(text);
      myButton.addActionListener(this);
      f.add(myButton);
      myLabel = new Label(text);
      f.add(myLabel);
      f.pack();
      f.setVisible(true);
   }
   public void actionPerformed(ActionEvent e) {
      if (text.equals("On")) text = "Off";
      else text = "On"; 
      myButton.setLabel(text);
      myLabel.setText(text);
   }
}

If you run this example, you will get:

AWT Button Action Handler
AWT Button Action Handler

The button works nicely. If you click the button, the button label text will change from "On" to "Off", and the text of the label component will also change.



 

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

AWT (Abstract Windows Toolkit)

 What Is AWT (Abstract Windows Toolkit)

 HelloAWT.java - My First AWT Program

 Closing AWT Frame and Terminating Application

 Drawing Graphics - Using paint() on Frame or Component

 Creating Labels with java.awt.Label Class

 Creating Buttons with java.awt.Button Class

 AWT Button Action Handler at the Component Level

AWT Button Action Handler at the Frame Level

 AWT Button Mouse Click Handler at the Frame Level

 AWT TextField and ActionListener

 MenuBarTest.java - AWT Menu Bar Test Program

 MenuTest.java - AWT Menu Test Program

 MenuItemTest.java - AWT Menu Item Test Program

 MenuItemActionListenerTest.java - AWT Menu Item Action Listener

 Integration with Desktop System

 Outdated Tutorials

 References

 Full Version in PDF/EPUB