Friday, October 5, 2007

MOre JAVA Tips



Event Handling

  • All of the events dispatched by AWT’s components use event classes that are subclasses of AWTEvent, which is in java.awt. These subclasses are defined in the java.awt.event package.
  • The AWT does not notify your event handler of every event that occurs over a component, as in the old days of 1.0.2. Now, AWT informs your event handler only about the events it is interested in.
  • Define a class which implements the necessary Listeners and then use the Component addABCListener() method to register an interest in a certain type of event, e.g. addMouseListener(this).
  • The Listener interfaces inherit directly from java.util.EventListener.
  • The Listener interfaces are
            • ActionListener
            • AdjustmentListener
            • ComponentListener
            • FocusListener
            • ItemListener
            • KeyListener
            • MouseListener
            • MouseMotionListener
            • TextListener
            • WindowListener

If you define a class which implements a listener, you must obviously include stubs for those listener methods not implemented as interfaces are implicitly abstract and failure to implement all of the methods would result in an abstract subclass.

  • The Listener methods are as follows: (Note - all are declared as public void and all take only one parameter which is an Event of the same name as the Listener, i.e. ActionListener methods take an ActionEvent etc.


Listener

Methods

ActionListener

actionPerformed

AdjustmentListener

adjustmentPerformed

ComponentListener

componentHidden

componentMoved

componentResized

componentShown

ContainerListener

componentAdded

componentRemoved

FocusListener

focusGained

focusLost

ItemListener

itemStateChanged

KeyListener

keyPressed

keyReleased

keyTyped

MouseListener

mouseClicked

mouseEntered

mouseExited

mousePressed

mouseReleased

MouseMotionListener

mouseDragged

mouseMoved

TextListener

textValueChanged

WindowListener

windowClosed

windowClosing

windowDeiconified

windowActivated

windowDeactivated

windowIconified

windowOpened



  • There’s an Adapter class to match each Listener interface. Each Adapter class defines no-op stubs for the methods declared in the corresponding interface. So can extend an Adapter rather than implement a Listener interface (but this only makes sense when the object that will listen for and handle the event has no other responsibilities).

Passing Arguments to Programs

  • The main() method must be declared as a public, static method that does not return a value and takes an array of String objects. The order of public and static, and the way in which the String array is defined, is not strictly enforced.
  • When main() ends, that may or may not be the end of the program. The JVM will run until the only remaining threads are daemon threads. If main() does not spawn any more threads, then the program will end when main() ends.

No comments: