[go: up one dir, main page]

0% found this document useful (0 votes)
38 views4 pages

Event Handling Event-Delegation Model: Setactioncommnand ("Add")

Event Handling Docs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Event Handling Event-Delegation Model: Setactioncommnand ("Add")

Event Handling Docs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Event Handling

Event-Delegation Model
In Java, events are objects. The java.awt.event. package defines a rich hierarchy of
event types. Through this package, instances of the various event classes are
constructed when users are GUI components. It is up to the programmer to decide
how to handle the generated event. For example, when a user clicks a button, the
system constructs an instance of class ActionEvent, in which it stores details about the
event (when, where, and so on). At this point, the programmer has three options:

 Ignore the event.


 Have the event handled by the component (in this case, a button) where the
event originated.
 Delegate event handling to some other object, possibly yourself, or objects called
listeners.

Sample 1: Handling the Event in the Originating Component

// SelfButton.java
import java.awt.*;
import java.awt.event.*;

public class SelfButton extends Button


{
public SelfButton( String sLabel )
{
super( sLabel );
enableEvents( AWTEvent.ACTION_EVENT_MASK );
}

public void processActionEvent( ActionEvent eAction )


{
super.processActioNEvent( eAction );
System.out.println( "Action!" );
}
}

Sample 2.1: Delegating the event.


The process of assigning an object to handle a component’s events is called
“delegation”. The event-handling objects are called “listeners”.

// ButtonDelegateTest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonDelegateTest extends JDialog


{
ButtonDelegateTest()
{
setTitle( "Button Delegate Test" );
setSize( 540, 380 );

getContentPane().setLayout( new FlowLayout() );


TestListener event1 = new TestListener();

JButton oButton = new JButton( "PRESS ME PLEASE" );


oButton.addActionListener( event1 );
oButton.setActionCommnand(“Add”);
getContentPane().add( oButton );
addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent eWindow )
{
System.exit( 0 );
}
}
);
}

class TestListener implements ActionListener


{
public void actionPerformed( ActionEvent eAction )
{
if (eAction.getActionCommand().equals(“Add”))
System.out.println( "You just pressed me!!!" );
}
}

public static void main( String[] args )


{
ButtonDelegateTest oDialog = new ButtonDelegateTest();
oDialog.setVisible( true );
}
}

Example 2.2;

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

public class TestEvents implements ActionListener


{
Button btn1, btn2, btn3;
Frame tagFrame;

TestEvents (){
tagFrame = new JFrame();

btn1 = new JButton(“Press button 1 please…”);


btn2 = new JButton(“Press button 2 please…”);
btn3 = new JButton(“Press button 3 please…”);

public void launchFrame(){


tagFrame.setTitle( "Event handling sample…");
tagFrame.setSize( 800, 500 );
tagFrame.setLayout(new FlowLayout());

TagFrame.add(btn1);
TagFrame.add(btn2);
TagFrame.add(btn3);

btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);

tagFrame.setVisible(true);
}

public void actionPerformed(ActionEvent evt){


if (evt.getSource() == btn1){
//event handled by button 1
System.out.println( "Button 1 just pressed!!!" );
} else if(evt.getSource() == btn2){
//event handled by button 2
System.out.println( "Button 2 just pressed!!!" );
}else{
//event handled by button 3
System.out.println( "Button 3 just pressed!!!" );
}
}
public static void main(String[] args) {
TestEvents ex = new TestEvents();
Ex.launchFrame();
}

Event Listener (java.awt.event )

Interface MouseListener
public interface MouseListener
extends EventListener

The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component. (To track
mouse moves and mouse drags, use the MouseMotionListener.)

Method Summary

 void mouseClicked(MouseEvent e)
          Invoked when the mouse has been clicked on a component.

 void mouseEntered(MouseEvent e)
          Invoked when the mouse enters a component.

 void mouseExited(MouseEvent e)
          Invoked when the mouse exits a component.

 void mousePressed(MouseEvent e)
          Invoked when a mouse button has been pressed on a component.

 void mouseReleased(MouseEvent e)
          Invoked when a mouse button has been released on a component.

Method Detail

mouseClicked
public void mouseClicked(MouseEvent e)
Invoked when the mouse has been clicked on a component.

mousePressed
public void mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.

mouseReleased
public void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component.

mouseEntered
public void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component.

mouseExited
public void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.

Interface ActionListener
public interface ActionListener
extends EventListener

The listener interface for receiving action events. The class that is interested in processing an action event implements this
interface, and the object created with that class is registered with a component, using the component's addActionListener method.
When the action event occurs, that object's actionPerformed method is invoked.

Method Summary

 void actionPerformed(ActionEvent e)
          Invoked when an action occurs.

Method Detail

actionPerformed
public void actionPerformed(ActionEvent e)
Invoked when an action occurs.

Interface TextListener
public interface TextListener
extends EventListener

The listener interface for receiving text events. The class that is interested in processing a text event implements this interface. The
object created with that class is then registered with a component using the component's addTextListener method. When the
component's text changes, the listener object's textValueChanged method is invoked.

Method Summary

 void textValueChanged(TextEvent e)
          Invoked when the value of the text has changed.

Method Detail

textValueChanged
public void textValueChanged(TextEvent e)
Invoked when the value of the text has changed. The code written for this method performs the operations that need to
occur when text changes.

You might also like