Key Listener

  • The listener interface for receiving keyboard events which include (keypressed, keyReleased, KeyTyped).
  • The class that is interested in processing a keyboard event either implements this interface (and all the methods it contains) or extends the abstract KeyAdapter class (overriding only the methods of interest).
Methods of Key Listener Interface
Method Description
keyPressed() Invoke when Key pressed
keyReleased(KeyEvent e) Invokes when Key Released.
keyTyped(KeyEvent e) Invokes when Key Typed.
Example :
package com.techknow.keylistener;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

public class KeyListnerDemo extends Frame
{
	private TextField textField1 = new TextField();
	private Label label1 = new Label();
	private Label label2 = new Label();
	private TextField textField2 = new TextField();
	private Label label3 = new Label();
	public KeyListnerDemo()
	{
		try
		{
			jbInit();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	private void jbInit() throws Exception
	{
		this.setLayout( null );
		this.setSize(new Dimension(389, 160));
		this.setBackground( SystemColor.control );
		this.setResizable(false);
		this.setTitle("Key Listener Demo");
		this.setVisible(true);
		this.setLocation(150,150);
		textField1.setBounds(new Rectangle
			(120, 65, 220, 20));
		textField1.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				textField1_keyPressed(e);
			}
		});
		label1.setText("Only Numbers");
		label1.setBounds(new Rectangle
			(15, 60, 100, 25));
		label1.setFont(new Font("Tahoma", 1, 12));
		label1.setForeground(java.awt.Color.red);
		label2.setText("Only Characters");
		label2.setBounds(new Rectangle
			(10, 100, 105, 20));
		label2.setFont(new Font("Tahoma", 1, 12));
		label2.setForeground(java.awt.Color.red);
		textField2.setBounds(new Rectangle
			(120, 105, 220, 20));
		textField2.addKeyListener(new KeyAdapter()
		{
			public void keyTyped(KeyEvent e)
			{
				textField2_keyTyped(e);
			}
		});
		label3.setText
		("Restriction on Text Field with Key Listener");
		label3.setBounds(new Rectangle(25, 35, 315, 15));
		label3.setFont(new Font("Tahoma", 1, 14));
		label3.setForeground(java.awt.Color.blue);
		this.add(label3, null);
		this.add(textField2, null);
		this.add(label2, null);
		this.add(label1, null);
		this.add(textField1, null);
	}
	private void textField1_keyPressed(KeyEvent e)
	{
		char c = e.getKeyChar();  
		if (!(Character.isDigit(c) ||  
		(c == KeyEvent.VK_BACK_SPACE) ||  
		(c == KeyEvent.VK_DELETE)))
		{  
			e.consume(); 
		}
	}
	private void textField2_keyTyped(KeyEvent e)
	{
		char c = e.getKeyChar();  
		if ((Character.isDigit(c) ||  
		(c == KeyEvent.VK_BACK_SPACE) ||  
		(c == KeyEvent.VK_DELETE)))
		{  
			e.consume();  
		}
	}
	public static void main(String args[])
	{
		new KeyListnerDemo();
	}
}
Output :
Key Listener

Mouse Listener

  • The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component. (To track mouse moves and mouse drags, use the MouseMotionListener.)
  • A mouse event is also generated when the mouse cursor enters or leaves a component. When a mouse event occurs, the relevant method in the listener object is invoked, and the MouseEvent is passed to it.
Methods of Mouse Listener Interface
Method Description
mouseClicked(MouseEvent e) Invokes when Mouse Click.
mouseEntered(MouseEvent e) Invokes when Mouse Entered.
mouseExited(MouseEvent e) Invokes when Mouse Exited.
mousePressed(MouseEvent e) Invokes when Mouse Pressed.
mouseReleased(MouseEvent e) Invokes when Mouse Released.
Example :
package com.techknow.mouselistener;

import java.awt.Button;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseListenerDemo extends Frame
{
	private int count=0;
	private TextField textField1 = new TextField();
	private Label label1 = new Label();
	private Label label2 = new Label();
	private Label label3 = new Label();
	private TextField textField2 = new TextField();
	public MouseListenerDemo()
	{
		try
		{
	    	jbInit();
		}
		catch (Exception e)
		{
	    	e.printStackTrace();
		}
	}
	private void jbInit() throws Exception
	{
		this.setLayout( null );
		this.setVisible(true);
		this.setLocation(150,150);
		this.setSize(new Dimension(386, 200));
		this.setBackground( SystemColor.control );
		this.setResizable(false);
		this.setTitle("Mouse Listener Demo");
		this.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				this_mouseClicked(e);
			}
		});
		textField1.setBounds(new Rectangle
			(185, 75, 165, 25));
		label1.setText("Number of click on Frame ");
		label1.setBounds(new Rectangle
			(10, 80, 170, 15));
		label1.setFont(new Font("Tahoma", 1, 12));
		label1.setForeground(java.awt.Color.blue);
		label2.setText("Check mouse count and Location");
		label2.setBounds(new Rectangle
			(40, 45, 290, 15));
		label2.setFont(new Font("Tahoma", 1, 14));
		label2.setForeground(java.awt.Color.red);
		label3.setText("Location of Mouse Cursor ");
		label3.setBounds(new Rectangle
			(10, 115, 170, 15));
		label3.setForeground(java.awt.Color.blue);
		label3.setFont(new Font("Tahoma", 1, 12));
		textField2.setBounds(new Rectangle
			(185, 110, 165, 25));
		this.add(textField2, null);
		this.add(label3, null);
		this.add(label2, null);
		this.add(label1, null);
		this.add(textField1, null);
	}
	private void this_mouseClicked(MouseEvent e)
	{
		int x=e.getX();
		int y=e.getY();
		count=count+1;
		textField1.setText(" "+count);
		textField2.setText("X-axis: "+ x + " , Y-axis:"+y);
	}
	public static void main(String args[])
	{
		new MouseListenerDemo();
	}
}
Output :
Mouse Listener

Window Listener

  • The listener interface for receiving window events.
  • The listener object created from that class is then registered with a Window using the window's addWindowListener method.
Methods of Window Listener Interface
Method Description
windowActivated(WindowEvent e) Invokes when Window Activated.
windowClosed(WindowEvent e) Invokes when Window Closed.
windowClosing(WindowEvent e) Invokes when Window going to close.
windowDeactivated(WindowEvent e) Invokes when Window deactivated.
windowDeiconified(WindowEvent e) Invokes when Window deiconified.
windowIconified(WindowEvent e) Invokes when Window iconified.
windowOpened(WindowEvent e) Invokes when Window opened.
Example :
package com.techknow.windowlistener;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;

public class WindowListenerDemo extends Frame
{
	private Label label1 = new Label();
	public WindowListenerDemo()
	{
		try
		{
	    	jbInit();
	    }
	    catch (Exception e)
	    {
	    	e.printStackTrace();
	    }
	}
	private void jbInit() throws Exception
	{
		this.setLayout( null );
		this.setVisible(true);
		this.setLocation(250,200);
		this.setSize(new Dimension(419, 158));
		this.setBackground( SystemColor.control );
		this.setTitle("Window Listener Demo");
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				this_windowClosing(e);
			}
			public void windowOpened(WindowEvent e)
			{
		    	this_windowOpened(e);
			}
		});
		label1.setText("All minimize, maximize 
		and also closing window are working...");
		label1.setBounds(new Rectangle
			(20, 0, 390, 130));
		label1.setFont(new Font("Tahoma", 1, 12));
		label1.setForeground(java.awt.Color.red);
		this.add(label1, null);
	}
	private void this_windowClosing(WindowEvent e)
	{
		System.exit(0); 
	}
	public static void main(String args[])
	{
		new WindowListenerDemo();
	}
	private void this_windowOpened(WindowEvent e)
	{
		JOptionPane.showMessageDialog
		(this,"Welcome To Techknow Heights");
	}
}
Output :
Window Listener