Focus Listener Event in Advanced Java Programming

Home  »  Java  »  Advance Java »    Java Event Handling »    FocusListener

Focus events are sent when a component gains or loses keyboard input focus. The following is the definition of the FocusEvent class.





A component can process its own focus events by calling
enableEvents(AWTEvent.FOCUS_EVENT_MASK) and providing a processFocusEvent() method. Alternatively, you can delegate focus events to listener that implements the FocusListener interface.



The following applet uses a BorderLayout manager to put a text field at "north" and a textarea at "center". The textfield delegates its focus events to the applet. The text area is a sub-class that handles its own focus events. Depending upon whether focus gained or lost, both the event handlers print a message to the console.



The above code will be stored in the file called FocusListenerTest.java, which on compilation will crate a class file called FocusListenerTest.class. Now compile this file using below commands in the DOS mode:



C:\>jdk1.4\bin>javac FocusListenerTest.java
C:\>jdk1.4\bin>appletviewer FocusListenerTest.java


"Output of FocusListenerTest.class"
"Output of FocusListenerTest.class in DOS mode"


Download Complete Program

  -  



Event Listener


Action Listener Adjustment Listener Mouse Listener
Mouse Motion Listener Window Listener Key Listener
Focus Listener ITEM Listener Text Listener



No comments:

Post a Comment