[ Back | Previous | Next ]

Creating and implementing own events

Package:
java.util.*
Product:
JDK
Release:
1.0.2
Related Links:
Calendar
EventObject
Hashtable
Locale
Properties
PropertyResourceBundle
TimeZone
Comment:
Creating own eventsobject to handle personal events. 

These three classes form the eventObject handling. What we have is this; class OwnEvent, MyEvent and
OwnEventListener. 

    1.class OwnEvent - is the main application and contains a textfield and a button myquery. Myquery has a
       normal ActionAdapter and open a new object MyEvent. 
    2.class MyEvent - is the application wherefrom an event is invoked to the main application OwnEvent. The
       application contains a textfield, my and a button, search ('Submit-query'). The objective is to send the
       query in textfield my to the main app. by pressing the search button in MyQuery. 
    3.class OwnEventListener - is the interface between the two app's and transports the EventObject. 

Lets discuss the flow: 

    1.in OwnEvent, a GUI is created with a textfield (search) and a button (myquery) 
    2.in OwnEvent, an actionlistener is connected to the myquery button event and when pressed it opens a new
       MyEvent object 
    3.in OnwEvent, pressed the my-query button, new MyEvent with the OwnEventListener because OwnEvent
       implements the OwnEventListener in its declaration. 
    4.in MyEvent, a GUI is created with a textfield (my) and a button (search) 
    5.in MyEvent, an actionlistener is connected to the search button event. 
    6.in MyEvent, when search button pressed the performs the actionPerformed method. In this method over
       the OwnEventListener interface an EventObject is send to the OwnEventListener (the OwnEvent.class) 
    7.in OwnEvent, it receive an request from the OwnEventListener and invokes the ownEventPerformed()
       method and receive with the Event the textfield as an Object and sets the (search) TextField in
       OwnEvent. 

//
// OwnEvent.java
//
 import java.awt.*;
 import java.awt.event.*; 
 import java.util.EventObject;

 public class OwnEvent extends Frame implements ActionListener,
                                                OwnEventListener {
   TextField search;
   Button myevent;  
   MyEvent me = null; 

 public OwnEvent() {
   this.setTitle("OwnEvent");
   this.setSize(300,300);
   init();
 }
     
 public void init() {            
   setBackground( Color.yellow );
   Panel mia = new Panel();                      
   search = new TextField(" Search", 30);
   mia.add( search );  
   mia.add( myevent = new Button(" MyEvent") );
   myevent.addActionListener( this );  
   add( mia );   
   addWindowListener( new AppAdapter() );
   this.setVisible( true );
 }  

 public void actionPerformed(ActionEvent ae) {
   Object source = ae.getSource();  
   String arg = ae.getActionCommand();  
   System.out.println("" + source + "--> " + arg );
   if ( source instanceof Button && arg.equals(" MyEvent") ) {  
     if (me == null )
       me = new MyEvent( this  );      
     }
   }
 }                     

 public void ownEventPerformed(EventObject e) {
   Object source = e.getSource();  
   String arg = (String) source.toString();  
   System.out.println("" + source + " --> " + arg ); 
   search.setText( ((TextField) source).getText() );
 } 
                                           
 //
 // WINDOW HANDLING
 //
 class AppAdapter extends WindowAdapter {
   public void windowClosing(WindowEvent event) {
     setVisible( false );
     dispose(); 
     System.exit(0);
  } 
 }

 public static void main ( String [] args ) {
   OwnEvent oe = new OwnEvent();
 }                  

 }


/ // MyEvent.java //
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
public class MyEvent extends Frame { 
   TextField my;
   ActionListener oel; 
   public MyEvent(ActionListener listener) { 
     this.oel = listener; this.setTitle("MyEvent"); this.setSize(300,300); 
     OwnEventButton search; 
     Panel me = new Panel(); 
     me.add( my = new TextField( "myquery",25 ));
     me.add( search = new OwnEventButton(" Submit Query ") );
    add( me ); 
    search.addActionListener( listener ); 
    addWindowListener( new AppAdapter() ); 
    this.setVisible( true ); 
} // // WINDOW HANDLING
// class AppAdapter extends WindowAdapter { 
         public void windowClosing(WindowEvent event) { setVisible( false ); dispose(); } } } 


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//
// OwnEventButton.java
//                    
                
import java.awt.*;
import java.awt.event.*;
public class OwnEventButton extends Button {
   
   ActionListener actionListener;

        public OwnEventButton( String name ) {
                super( name );
        }                 

  /**
   * Adds the specified action listener to receive action events
   * from this button.
   * @param listener the action listener
   */
   public void addActionListener(ActionListener listener) {
       actionListener = AWTEventMulticaster.add(actionListener, listener);
       enableEvents(AWTEvent.MOUSE_EVENT_MASK);
   }                  
 
   /**
    * Removes the specified action listener so it no longer receives
    * action events from this button.
    * @param listener the action listener
    */
   public void removeActionListener( ActionListener listener) {
       actionListener = AWTEventMulticaster.remove(actionListener, listener); 
   }            

   /**
    * Paints the button and distribute an action event to all listeners.
    */
   public void processMouseEvent(MouseEvent e) {
       switch(e.getID()) {
          case MouseEvent.MOUSE_RELEASED:
            if(actionListener != null) {
               actionListener.actionPerformed(new ActionEvent(
                   this, ActionEvent.ACTION_PERFORMED, this.getLabel()));
            }             
            break;
       }
       super.processMouseEvent(e);
   }
}
1