Total members 11895 |It is currently Tue Dec 03, 2024 5:47 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





What is Event Handling? How to use events in Java
--------------------------------------------


Almost all programs must respond to commands from the user in order to be useful. Java's AWT uses event driven programming to achieve processing of user actins: one that underlies all modern window systems programming. Within the AWT, all user actions belong to an abstract set of things called events. An event describes, in sufficient detail, a particular user action. Rather than the program actively collecting user-generated events, the Java run time notifies the program when an interesting event occurs. Programs that handle user interaction in this fashion are said to be event driven.

There are three parts to the event model in Java:

Event object - this is an instance of a Java class that contains the characteristics of the event. For example, if the event is generated from clicking a mouse button, the event object would contain information such as the coordinates of the mouse cursor, which button was clicked, how many times it was clicked, etc.

Dispatching class/method - this is an object, which detects that an event has occurred and is then responsible for notifying other objects of the event, passing the appropriate event object to those objects. These other objects are "listeners" for the event. Most AWT components, such as Button, List, Textfield, etc. are examples of dispatching classes.

A Button, for instance, is capable of notifying other components when it is "pushed." These classes will typically have a set of two methods that can be invoked by would-be "listeners": one to tell the class that the object wants to listen and another to tell the class that the object no longer wants to listen.

These methods are conventionally named i.e. addxxListener (to add an object as a listener) or removexxListener (to remove the object as a listener). Here "xx" is the specific type of event to listen for. In the case of the Button, this would be "Action" to indicate the action of pushing the button. (So the methods would be addActionListener and removeActionListener).

Listener Interface - for the dispatching to work properly, the dispatching class must be able to rely on each of its listeners to contain the method that it executes when the event occurs. This is easily accomplished in Java through the use of an Interface class. The important point is that a class, which is going to be a listener, must implement that interface.

Example of using Action Listener with buttons on a JPanel
java code
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

public class MyPanel extends JPanel
{
public FileInputStream input = null;
public FileOutputStream output = null;
String result="LZW Compression and Decompression Program";

public MyPanel()
{

JButton compress=new JButton("Compress");
JButton decompress=new JButton("Decompress");
JButton exit =new JButton("Exit");

add(compress);
add(decompress);
add(exit);

compress.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JFileChooser chooser =new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.showOpenDialog(null);
String filename=chooser.getSelectedFile().getPath();
try
{
input = new FileInputStream(filename);
}
catch ( FileNotFoundException fnfe )
{
System.out.println( "Unable to open Input file: "+filename);
System.exit( 1 );
}
try
{
output = new FileOutputStream( "output.txt" );
}
catch ( FileNotFoundException fnfe )
{
System.out.println( "Unable to open output file output.txt " );
System.exit( 1 );
}
LZWCompression lzw = new LZWCompression(input,output);
lzw.compress();

result=" Done! Successfuly compression";
repaint();
}
});

decompress.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JFileChooser chooser =new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.showOpenDialog(null);
String filename=chooser.getSelectedFile().getPath();
try
{
input = new FileInputStream(filename);
}
catch ( FileNotFoundException fnfe )
{
System.out.println( "Unable to open Input file: "+filename);
System.exit( 1 );
}
try
{
output = new FileOutputStream( "output.txt" );
}
catch ( FileNotFoundException fnfe )
{
System.out.println( "Unable to open output file output.txt " );
System.exit( 1 );
}
LZWCompression lzw = new LZWCompression(input,output);
lzw.decompress();

result=" Done! Successfuly Decompression";
repaint();
}
});

exit.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
input.close();
output.close();
}
catch ( IOException ioe )
{
System.out.println( "IOException in main()." );
System.exit(1);
}
System.exit(0);
}
});


}


}


For mouse events it may be something like this
java code
public MainPanel(ToolPanel myToolPanel,StatusPanel myStatusPanel) {

setSize(600,600);
setLocation(3,10);

MousewhenMove mouseDragAndDrop=new MousewhenMove();
Mousehere mouseHereEvent=new Mousehere();
addMouseMotionListener(mouseDragAndDrop);
addMouseListener(mouseHereEvent);



}


where for example
java code
private class  Mousehere implements MouseListener {

public void mouseClicked(MouseEvent e) {


}

public void mousePressed(MouseEvent e) {


}
public void mouseReleased(MouseEvent e) {
}

}


class MousewhenMove implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {


}
public void mouseMoved(MouseEvent e) {

}


}


For sure you can define the mouse events listener inline such as action listener.



_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : What is Event Handling?
 Event Handling Notes     -  
 ComponentListener event handling     -  
 Container Event handling with ContainerListener     -  
 explain event handling in java     -  
 java.util classes and interfaces support event handling     -  
 advantage of the event delegation model over event-inherit     -  
 relationship an event-listener interface & event handler     -  
 AWT event hierarchy     -  
 Thread of Event Dispatcher     -  
 event results from the clicking of a button     -  



Topic Tags

Java Events






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com