?
Solved

action(Event evt, Object arg)

Posted on 1999-12-23
7
Medium Priority
?
230 Views
Last Modified: 2012-05-04
as i know above method already deprecated at java 1.2.2,
so is it ActionListener is the alternative for it?
how to use it?
0
Comment
Question by:eguy
  • 3
  • 3
7 Comments
 
LVL 7

Expert Comment

by:vladi21
ID: 2304351
JDK 1.0 to 1.1: Making the Transition
Listening for Events
http://developer.java.sun.com/developer/technicalArticles/GUI/Transition/eventlist.html

JavaTM AWT: Delegation Event Model
http://java.sun.com/products//jdk/1.1/docs/guide/awt/designspec/events.html


from Java in Nutshell

The Java 1.1 event model is used by both the AWT and by Java Beans. In this model, different classes of events are represented by different Java classes. Every event is a subclass of java.util.EventObject. AWT events, which is what we are concerned with here, are subclasses of java.awt.AWTEvent. For convenience, the various types of AWT events, such as MouseEvent and ActionEvent, are placed in the new java.awt.event package.

Every event has a source object, which can be obtained with getSource(), and every AWT event has a type value, which can be obtained with getID(). This value is used to distinguish the various types of events that are represented by the same event class. For example, the FocusEvent has two possible types: FocusEvent.FOCUS_GAINED and FocusEvent.FOCUS_LOST. Event subclasses contain whatever data values are pertinent to the particular event type. For example, MouseEvent has getX(), getY(), and getClickCount() methods; it also inherits the getModifiers() and getWhen() methods, among others.

The 1.1 event handling model is based on the concept of an "event listener." An object interested in receiving events is an event listener. An object that generates events (an event source) maintains a list of listeners that are interested in being notified when events occur, and provides methods that allow listeners to add themselves and remove themselves from this list of interested objects. When the event source object generates an event (or when a user input event occurs on the event source object), the event source notifies all the listener objects that the event has occurred.

An event source notifies an event listener object by invoking a method on it and passing it an event object (an instance of a subclass of EventObject). In order for a source to invoke a method on a listener, all listeners must implement the required method. This is ensured by requiring that all event listeners for a particular type of event implement a corresponding interface. For example, event listener objects for ActionEvent events must implement the ActionListener interface. The java.awt.event package defines an event listener interface for each of the event types it defines. (Actually, for MouseEvent events, it defines two listener interfaces: MouseListener and MouseMotionListener.) All event listener interfaces themselves extend java.util.EventListener. This interface does not define any methods, but instead acts as a marker interface, clearly identifying all event listeners as such.

An event listener interface may define more than one method. For example, an event class like MouseEvent represents several different types of mouse events, such as a button press event and a button release event, and these different event types cause different methods in the corresponding event listener to be invoked. By convention, the methods of an event listener are passed a single argument, which is an event object of the type that corresponds to the listener. This event object should contain all the information a program needs to respond to the event. Table 7.6 lists the event types defined in java.awt.event, the corresponding listener (or listeners), and the methods defined by each listener interface.

.....
For each of the event listener interfaces that contains more than one method, java.awt.event defines a simple "adapter" class that provides an empty body for each of the methods in the interface. When you are only interested in one or two of the defined methods, it is sometimes easier to subclass the adapter class than it is to implement the interface. If you subclass the adapter, you only have to override the methods of interest, but if you implement the interface directly you have to define all of the methods, which means you must provide empty bodies for all the methods that are not of interest. These pre-defined no-op adapter classes bear the same name as the interfaces they implement, with "Listener" changed to "Adapter": MouseAdapter, WindowAdapter, etc.

Once you have implemented a listener interface, or subclassed a adapter class, you must instantiate your new class to define an individual event listener object. You then register that listener with the appropriate event source. In AWT programs, an event source is always some sort of AWT component. Event listener registration methods follow a standard naming convention: if an event source generates events of type X, it has a method named addXListener() to add an event listener, and a method removeXListener() to remove a listener. One of the nice features of the 1.1 event model is that it is easy to determine the types of events a component can generate--just look for the event listener registration methods. For example, by inspecting the API of the Button object, you can determine that it generates ActionEvent events. Table 7.7 lists AWT components and the events they generate.




0
 

Author Comment

by:eguy
ID: 2305379
pls give example how to use it,i mean if i create a button,how to use it to listen the event.thanks
0
 
LVL 2

Accepted Solution

by:
meming earned 360 total points
ID: 2305567
Here is a straight answer:

class A
{
 ...
 JButton jb = new JButton();
 jb.add(new PopupMenuActBtnListener());

 class PopupMenuActBtnListener implements ActionListener
 {
  public void actionPerformed (ActionEvent e)
  {
   doIt(e.getActionCommand()); //do your event handling here
  }
 }
....
}

OR its equivalent

class A
{
 ...
 JButton jb = new JButton();
 jb.add(new ActionListener()
 {
  public void actionPerformed (ActionEvent e)
  {
   doIt(e.getActionCommand()); //do your event handling here
  }
 })
....
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:eguy
ID: 2305970
thanks..if don wan to use swing(use awt onli) can????
0
 
LVL 2

Expert Comment

by:meming
ID: 2305979
Sure you can. ActionListener is actually part of the java.awt.event.* package.
0
 

Author Comment

by:eguy
ID: 2306647
how to use?
could u give one simple example with button ,thanks
0
 
LVL 2

Expert Comment

by:meming
ID: 2306899
Just take "J" off the Button class name. This remains true for many AWT-Swing widget pairs.

class A
             {
              ...
              Button b = new Button();
              b.add(new PopupMenuActBtnListener());

              class PopupMenuActBtnListener implements ActionListener
              {
               public void actionPerformed (ActionEvent e)
               {
                doIt(e.getActionCommand()); //do your event handling here
               }
              }
             ....
             }

             OR its equivalent

             class A
             {
              ...
              Button b = new Button();
              b.add(new ActionListener()
              {
               public void actionPerformed (ActionEvent e)
               {
                doIt(e.getActionCommand()); //do your event handling here
               }
              })
             ....
             }
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month5 days, 5 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question