Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Awt Event handling in Java AWT

Related Tutorials


 
 

Share on Google+Share on Google+

Event handling in Java AWT

Advertisement
In this section, you will learn how to handle events in java awt. Here, this is done through the java.awt.*; package of java.

Event handling in Java AWT

     

Introduction

In this section, you will learn how to handle events in java awt. Here, this is done through the java.awt.*; package of java. Events are the integral part of the java platform. You can see the concepts related to the event handling through this example and used methods through which you can implement the event driven application.

This example shows you how to handle events in java.awt.*; package. AwtEvent is the main class of the program which extends from the Frame class implements the ActionListener interface.

This program starts to run from the main method in which the object for the AwtEvent class has been created. The constructor of the AwtEvent class creates two buttons with adding the addActionListener() method to it. The constructor also initializes a label with text "Roseindia.net".

When you click on the button then the actionPerformed() method is called which receives the generated event. This method shows the text of the source of the event on the label.

Here is the code of the program : 

import java.awt.*;
import java.awt.event.*;

public class AwtEvent extends Frame implements ActionListener{
  Label lbl;
  public static void main(String argv[]){
  AwtEvent t = new AwtEvent();
  }
  
  public AwtEvent(){
  super("Event in Java awt");
  setLayout(new BorderLayout());
  try{
  Button button = new Button("INSERT_AN_URL_HERE");
  button.addActionListener(this);
  add(button, BorderLayout.NORTH);
  Button button1 = new Button("INSERT_A_FILENAME_HERE");
  button1.addActionListener(this);
  add(button1, BorderLayout.SOUTH);
  lbl = new Label("Roseindia.net");
  add(lbl, BorderLayout.CENTER);
  addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });
  }
  catch (Exception e){}
  setSize(400,400);
  setVisible(true);
  }
  
  public void actionPerformed(ActionEvent e){
  Button bt = (Button)e.getSource();
  String str = bt.getLabel();
  lbl.setText(str);
  }
}

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Related Tutorials

Discuss: Event handling in Java AWT  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:11
assad ali
October 22, 2011
event handler

my problem is that what this event handler code i write in the notepad and if i do this then also how i can run event handler code in JDK you send me any example that i can understand if this jdk not suit then which i software used to executing and compiling this code thanks .
Rajan kumar tiwari
October 31, 2011
event handling

i like this
Amit
June 26, 2012
Java

I want to ask some questions about java
Tahseen
September 9, 2012
hi

wat is the general syntax for this event handling????
apurva
September 17, 2012
java

any 4 interfaces used for event handling??
avaneesh
December 20, 2012
nothing

nice .......
MINAKSHI KUMARI
October 12, 2012
COMPUTER SCIENCE

SIR,I AM STUDENT OF MCA 5TH SEM.
saravanakumar
August 29, 2013
java awt

sir, what is the use of "BasicWindowMonitor".... explain the below statement addWindowListener(new BasicWindowMonitor()); s i got a bug in this line
saras
July 16, 2013
freeinfozone.com

a very nice defination with a nice and simple example. In future i am going to visit this site freequently so i can overcome my doubts in java. . . . . .
DMCA.com