HOME
J2ee tutorials
Java introduction
Java basic
Java installation
Java Packages Learn
Applets Java Threads Java Gui component

Java Event Handaling Java Streams and Files
 Java Swings Java JDBC
 Java Network Programming Java RMI
 Java Servlets Java javabeans
 EJB Struts

|
Event Handling
In the previous chapter many ways to develop effective GUI were discusses.
You must have felt that the GUI’s should interact with the user. For example when you click on certain button,
some text is displayed I the text box. To obtain such kind of functionality for the GUI’s java defines techniques called
event handling.
Now let’s discuss events
An event represents a user’s interaction with the program. When a user performs an action such as mouse click, the system creates an event and passes it to the program event handling methods. The event handling methods then respond with the relevant behavior for that event. For example when a user clicks submit button in a dialog, the data associated with a dialog is sent to the appropriate server location. Events are represented by object in java.
Technically speaking the user is the one who initiates the generation of the event. For example the user clicks the mouse button and generates an event. But it is the operating system that reads the clicks of the mouse button and generates the true event.
To create the event, the java programmer needs to do two things
- listen for the event required
- code the functionality required and the occurrence of that event.
Event source is the point of generation of the event. This would be the click of the mouse button or event the movement of the mouse on your applet. The mouse is the event source.
Event handler is simply a method that gets called whenever that particular event takes place.< br>
To generate an event and for the program to respond to the generated event, the listener interface needs to be implemented.
The steps for implementing the listener interface are
- create your own class and Implement Actionlistener
-
Register the listener
-
Provide implementation for all methods
Let us try to understand the event handling by following example
Example |
import java.awt.*;
import java.awt.event.*;
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
public class MyFrame3 extends Frame implements ActionListener
{
Button b1=new Button("green");
TextArea ta=new TextArea(10,20);
TextField tf=new TextField(10);
public MyFrame3(String t)
{
add(b1);
add(ta);
add(tf);
setLayout(new FlowLayout());
setTitle(t);
setSize(400,600);
setVisible(true);
b1.addActionListener(this);
addWindowListener(new MyAdapter());
}
public void actionPerformed(ActionEvent ae)
{
String str=null;
if(ae.getSource()==b1)
{
str=tf.getText();
ta.setText(str);
b1.setBackground(Color.green);
}
}
public static void main(String arg[])
{
MyFrame3 f1=new MyFrame3("my frame");
}
}
Example 2 |
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="KeyEvents" width=300 height=100>
</applet>
*/
public class KeyEvents extends Applet
implements KeyListener {
String msg = "";
int X = 10, Y = 20; // output coordinates
public void init() {
addKeyListener(this);
requestFocus(); // request input focus
}
public void keyPressed(KeyEvent ke) {
showStatus("Key Down");
int key = ke.getKeyCode();
switch(key) {
case KeyEvent.VK_F1:
msg += "";
break;
case KeyEvent.VK_F2:
msg += "";
break;
case KeyEvent.VK_F3:
msg += "";
break;
case KeyEvent.VK_PAGE_DOWN:
msg += "";
break;
case KeyEvent.VK_PAGE_UP:
msg += "";
break;
case KeyEvent.VK_LEFT:
msg += "";
break;
case KeyEvent.VK_RIGHT:
msg += "";
break;
}
repaint();
}
public void keyReleased(KeyEvent ke) {
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke) {
msg += ke.getKeyChar();
repaint();
}
// Display keystrokes.
public void paint(Graphics g) {
g.drawString(msg, X, Y);
}
}
|
|
Inform Friend About This Site |