File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Events" Watch "Events" New topic


Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
Hi all!
What code should i write in the following program so that when the button is pressed the label changes to "Hello all !".
I don't know how to write the action listeners.

import java.awt.*;
import java.awt.event.*;
public class hello1

public static void main(String args[])
Frame f=new Frame("Event testing !");
static Label l1=new Label("Hello World !");
Button b=new Button("Hit me!");
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Inside your main code after creating b you have two choices:
1. Use your class Hello1 to be the action listener:

Or you can use an anonymous class to perform the function:
public class Hello1
public static void main( String[] args )
Button b = new Button( "Hello" );
b.addActionListener( new ActionListener()
public void actionPerformed( ActionEvent ae )
// Perform label change here
ae.getSource().setLabel( "Changed" );
Good Luck,
It is sorta covered in the JavaRanch Style Guide.
subject: Events