You are on page 1of 7

Practical No-10

KeyListener Interface
KeyListener Interface

• The Java KeyListener is notified whenever you change the state of


key.
• It is notified against KeyEvent.
• The KeyListener interface is found in java.awt.event package, and it has
three methods.
• Declaration:
• public interface KeyListener extends EventListener
• Methods inherited
• This interface inherits methods from the following interfaces:
• java.awt.EventListener

• The class which processes the KeyEvent should implement this interface.
• The object of that class must be registered with a component.
• The object can be registered using the addKeyListener() method.
• Methods of KeyListener interface
• public abstract void keyPressed (KeyEvent e); - It is invoked when a
key has been pressed.

• public abstract void keyReleased (KeyEvent e); - It is invoked when a


key has been released.
• public abstract void keyTyped (KeyEvent e); -It is invoked when a key
has been typed.
repaint() Method
• The repaint method is a final method available in the Applet class, and that’s why
it cannot be overridden.
• Whenever the repaint method is to be used, it should be directly called from the
Applet class’s subclasses.
• The repaint method is responsible for handling update to the paint cycle of the
applet.
• Whenever we want a component to repaint itself, we need to call the repaint
method.
• In case we have made changes to the appearance of a component but have not
made any changes to its size, then we can call the repaint method to update the
new appearance of the component on the graphical user interface.
• Whenever the repaint method is invoked from a component, a request is
sent to the graphical user interface, which communicates to the graphical
user interface to perform some action at a future instance of time. The
whole idea behind the repaint method is to avoid the call to paint ()
method directly.
paint() Method
• The paint( ) method is called each time an AWT-based applet’s output must be
redrawn.
• paint( ) is also called when the applet begins execution.
• Whatever the cause, whenever the applet must redraw its output, paint( ) is
called. 
• The paint( ) method has one parameter of type Graphics.
• The method paint() is automatically called whenever there is a need to display an
applet window, this need could arise in certain situations –
• When an applet window is brought up on the screen for the first time.
• When an applet window is brought up on the screen from a minimized state, this
leads the redrawing of the applet window and hence the paint() method is
automatically called.
• When an applet window is stretched to a new size, this leads to redrawing of the
applet window to a new size and hence the paint() method is automatically
called.

You might also like