Here is a quick lesson on Event Handling in Java for those who begin Java Programming Language.This will illustrate every classes and interfaces you need to know.
Any action that user performs on a GUI component must be listened. For example, if a user clicks on a Close button, then we need to write code to close the program. So for this, we need to know that the user has clicked the button. This process of knowing is called as listening and the action done by the user is called an event. Writing the corresponding code for a user action is called as Event handling.
Examples on each Event
  1. Button
    1. First, you must import package to impletment the interface :import java.awt.event.*;
    2. Button cmdClose=new Button("Close"); cmdClose.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae)
      {
      // do something here
      }
      });
    ActionListener is an interface, so you must implement its method, and its method is actionPerfomred(ActionEvent e)
  2. Other Components that have the same Event are listed here:
    • MenuItem
    • TextField
    • List
    • ...
  3. Checkbox
  4. First, you must import package to impletment the interface :import java.awt.event.*;
    Checkbox c=new Checkbox("Check me");
    c.addItemListener(new ItemListener(){
    public void itemStateChanged(ItemEvent ie)
    {
    // Change frame title
    myFrame.setTitle("You "+(c.getState()?"checked":"unchecked")+".");
    }
    });
    ItemListener is an interface, so you must implement its method, and its method is itemStateChanged(ItemEvent e)
    Other controls which have this event are :
    • RadioButton
    • List
    • Choice

We hop you enjoy this lesson. More lessons are coming soon...

►►សូមអរគុណរាល់ការចូលរួមCommentរបស់អ្នក!

 
Top
Don't You Think this Awesome Post should be shared ??
| Event Handling in Java for Beginner |