0% found this document useful (0 votes)
28 views

Event Handling Java - Awt Package

The document discusses the event handling model in AWT. It describes event objects, event sources, event listeners, and how to attach event listeners to event sources. Specifically, it covers action events on buttons using the ActionListener interface, and provides examples of using action listeners on buttons. It also discusses mouse events using the MouseListener interface and MouseAdapter class, and provides an example of adding a mouse listener.

Uploaded by

Kirti Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Event Handling Java - Awt Package

The document discusses the event handling model in AWT. It describes event objects, event sources, event listeners, and how to attach event listeners to event sources. Specifically, it covers action events on buttons using the ActionListener interface, and provides examples of using action listeners on buttons. It also discusses mouse events using the MouseListener interface and MouseAdapter class, and provides an example of adding a mouse listener.

Uploaded by

Kirti Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Event Handling Model of AWT

Event object

Event handling
methods
Event source

Event listener

Action Events on Buttons


ActionEvent

actionPerformed(..)
Button

ActionListener

How to Attach an Event Listener


to an Event Source
o is an event source
h is an event listener of type XXX
o.addXXX(h)
where XXX is one of the following:
ComponentListener
ActionListener
FocusListener
MouseListener
MouseMotionListener

TextListener

KeyListener

AdjustmentListener

WindowListener

ItemListener

The ActionListener Interface


interface ActionListener {
public void actionPerformed(ActionEvent e);
}

Possible event sources


Button
List
TextField
MenuItem

Example
Hello
World
(version
1)
import java.awt.*;
import java.awt.event.*;
public class TestButtonAction {
public static void main(String[] args){
Frame f = new Frame("TestButton");
f.setSize(200,200);
Button hw = new Button("Hello World!");
f.add(hw);
hw.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
f.setVisible(true);
}
}

Example
Hello World (version 2)

class MyFrame extends Frame implements ActionListener {


Button hw;
public MyFrame(){
super("Test Button");
setSize(200,200);
hw = new Button("Hello World!");
add(hw);
hw.addActionListener(this);
show();
}
public void actionPerformed(ActionEvent o){
System.exit(0);
}
}

Example
Hello World (version 3)
class MyFrame extends Frame {
Button hw;
public MyFrame(){
super("Test Button");
setSize(200,200);
hw = new Button("Hello World!");
add(hw);
hw.addActionListener(new MyActionListener());
show();
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent o){
System.exit(0);
}
}
}
}

Revisit CardLayout
Flip Cards

MenuItem bp = new MenuItem("Flip to Button Panel");


bp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
CardLayout layout = (CardLayout)f.getLayout();
layout.show(f,"ButtonPanel");
}
});

How to Use FileDialog?


Menu m1 = new Menu("File");
MenuItem open = new MenuItem("Open"); m1.add(open);
mb.add(m1);
final TextArea ta = new TextArea(10,10);
f.add(ta);

open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog dialog = new FileDialog(f, "Open File", FileDialog.LOAD)
dialog.show();
ta.append(dialog.getFile()+"\n");
}
});

MouseListener

mouseClicked(MouseEvent)
Invoked when the mouse has been clicked on a component.
mouseEntered(MouseEvent)
Invoked when the mouse enters a component.
mouseExited(MouseEvent)
Invoked when the mouse exits a component.
mousePressed(MouseEvent)
Invoked when a mouse button has been pressed on a componen
mouseReleased(MouseEvent)
Invoked when a mouse button has been released on a compone

MouseAdapter
class MouseAdapter implements MouseListener {
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}

Example
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class TestMouseListener {
public static void main(String[] args){
Frame f = new Frame("TestMouseListener");
f.setSize(500,500);

f.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.out.println("Mouse clicked: ("+e.getX()+","+e.getY
}
....
}

MouseMotionListener
mouseDragged(MouseEvent)
Invoked when a mouse button is pressed on a
component and then dragged.

You might also like