- Event Object merupakan object yang mendiskripsikan sebuah event yang di-trigger oleh event source.
- Event Handler merupakan method yang menerima event object dan melakukan respond yang sesuai dengan event object tersebut.
- Event Listener merupakan interface yang akan meng-handle event yang terjadi. setiap type event mempunyai interface yang bersesuaian. Listener tersebut harus diimplementasikan oleh class yang akan meng-handle event
- Event Source merupakan pembangkit / trigger sebuah event objects
setiap event object mempunyai type event yang berbdea beda. sehingga kita harus menentukan type event sebelum menentukan jenis interface listener. karena setiap type event mempunyai jenis interface listener yang bersesuaian.
berikut ini type type event yang ada di Java :
Bagaimana event handling dalam Java? ada tiga langkah penting dalam event handling
MouseEventsDemo.java
package mouseeventsdemo;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Ren
*/
public class MouseEventsDemo extends Frame implements MouseListener, MouseMotionListener { // Langkah 1
TextField tf;
public MouseEventsDemo( String title){
super(title);
tf = new TextField(60);
addMouseListener(this); // Langkah 2
}
public void launchFrame() {
/* Menambah komponen pada frame */
add(tf, BorderLayout.SOUTH);
setSize(300,300);
setVisible(true);
}
public void mouseClicked(MouseEvent me) { // Langkah 3
String msg = “Mouse clicked.”;
tf.setText(msg);
}
public void mouseEntered(MouseEvent me) {
String msg = “Mouse entered component.”;
tf.setText(msg);
}
public void mouseExited(MouseEvent me) {
String msg = “Mouse exited component.”;
tf.setText(msg);
}
public void mousePressed(MouseEvent me) {
String msg = “Mouse pressed.”;
tf.setText(msg);
}
public void mouseReleased(MouseEvent me) {
String msg = “Mouse released.”;
tf.setText(msg);
}
public void mouseDragged(MouseEvent me) {
String msg = “Mouse dragged at ” + me.getX()
+ “,” + me.getY();
tf.setText(msg);
}
public void mouseMoved(MouseEvent me) {
String msg = “Mouse moved at ” + me.getX()
+ “,” + me.getY();
tf.setText(msg);
}
}
main.java
package mouseeventsdemo;
/**
*
* @author Ren
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MouseEventsDemo med = new MouseEventsDemo(“Mouse Events Demo”);
med.launchFrame();
}
}
berikut ini type type event yang ada di Java :
- ActionEvent
- ItemEvent
- WindowEvent
- ContainerEvent
- ComponentEvent
- FocusEvent
- TextEvent
- KeyEvent
- MouseEvent
- AdjustmentEvent
- ActionListener
- ItemListener
- WindowListener
- ContainerListener
- ComponentListener
- FocusListener
- TextListener
- KeyListener
- MouseListener
- MouseMotionListener
- AdjustmentListener
Bagaimana event handling dalam Java? ada tiga langkah penting dalam event handling
- Deklarasikan class yang akan meng-handle event yang terjadi dan tuliskan code yang menyatakan class tersebut meng-implement interface listener
- Event source mendaftarkan sebuah listener melalui method add<type>Listener
- Kode yang mengimplementasikan method pada interface listener pada class yang akan meng-handle event. [ Override ]
MouseEventsDemo.java
package mouseeventsdemo;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Ren
*/
public class MouseEventsDemo extends Frame implements MouseListener, MouseMotionListener { // Langkah 1
TextField tf;
public MouseEventsDemo( String title){
super(title);
tf = new TextField(60);
addMouseListener(this); // Langkah 2
}
public void launchFrame() {
/* Menambah komponen pada frame */
add(tf, BorderLayout.SOUTH);
setSize(300,300);
setVisible(true);
}
public void mouseClicked(MouseEvent me) { // Langkah 3
String msg = “Mouse clicked.”;
tf.setText(msg);
}
public void mouseEntered(MouseEvent me) {
String msg = “Mouse entered component.”;
tf.setText(msg);
}
public void mouseExited(MouseEvent me) {
String msg = “Mouse exited component.”;
tf.setText(msg);
}
public void mousePressed(MouseEvent me) {
String msg = “Mouse pressed.”;
tf.setText(msg);
}
public void mouseReleased(MouseEvent me) {
String msg = “Mouse released.”;
tf.setText(msg);
}
public void mouseDragged(MouseEvent me) {
String msg = “Mouse dragged at ” + me.getX()
+ “,” + me.getY();
tf.setText(msg);
}
public void mouseMoved(MouseEvent me) {
String msg = “Mouse moved at ” + me.getX()
+ “,” + me.getY();
tf.setText(msg);
}
}
main.java
package mouseeventsdemo;
/**
*
* @author Ren
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MouseEventsDemo med = new MouseEventsDemo(“Mouse Events Demo”);
med.launchFrame();
}
}