wood burning stoves 2.0*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Events" Watch "Events" New topic
Author

Events

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 !");
f.setSize(500,500);
static Label l1=new Label("Hello World !");
Button b=new Button("Hit me!");
f.add(b,BorderLayout.East);
f.add(l1,BorderLayout.WEST);
f.setVisible(true);
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi,
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:
[CODE]
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" );
}
});
}
}
[CODE]
Good Luck,
Manfred.
 
Don't get me started about those stupid light bulbs.
 
subject: Events