Skip to main content

JAVA ItemListener, KeyListener, WindowListener

Java ItemListener Interface

The Java ItemListener is notified whenever you click on the checkbox. It is notified against ItemEvent. The ItemListener interface is found in java.awt.event package. It has only one method: itemStateChanged().

itemStateChanged() method

The itemStateChanged() method is invoked automatically whenever you click or unclick on the registered checkbox component.

  1. public abstract void itemStateChanged(ItemEvent e);  

Java ItemListener Example

  1. import java.awt.*;    
  2. import java.awt.event.*;    
  3. public class ItemListenerExample implements ItemListener{    
  4.     Checkbox checkBox1,checkBox2;  
  5.     Label label;  
  6.     ItemListenerExample(){    
  7.         Frame f= new Frame("CheckBox Example");    
  8.         label = new Label();            
  9.         label.setAlignment(Label.CENTER);    
  10.         label.setSize(400,100);    
  11.         checkBox1 = new Checkbox("C++");    
  12.         checkBox1.setBounds(100,10050,50);    
  13.         checkBox2 = new Checkbox("Java");    
  14.         checkBox2.setBounds(100,15050,50);    
  15.         f.add(checkBox1); f.add(checkBox2); f.add(label);    
  16.         checkBox1.addItemListener(this);    
  17.         checkBox2.addItemListener(this);    
  18.         f.setSize(400,400);    
  19.         f.setLayout(null);    
  20.         f.setVisible(true);    
  21.      }    
  22.     public void itemStateChanged(ItemEvent e) {      
  23.         if(e.getSource()==checkBox1)  
  24.             label.setText("C++ Checkbox: "     
  25.             + (e.getStateChange()==1?"checked":"unchecked"));   
  26.         if(e.getSource()==checkBox2)  
  27.         label.setText("Java Checkbox: "     
  28.         + (e.getStateChange()==1?"checked":"unchecked"));    
  29.      }  
  30. public static void main(String args[])    
  31. {    
  32.     new ItemListenerExample();    
  33. }    
  34. }    

Output:

java awt itemlistener example 2

Java 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. It has three methods.

Methods of KeyListener interface

The signature of 3 methods found in KeyListener interface are given below:

  1. public abstract void keyPressed(KeyEvent e);  
  2. public abstract void keyReleased(KeyEvent e);  
  3. public abstract void keyTyped(KeyEvent e);  

Java KeyListener Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class KeyListenerExample extends Frame implements KeyListener{  
  4.     Label l;  
  5.     TextArea area;  
  6.     KeyListenerExample(){  
  7.           
  8.         l=new Label();  
  9.         l.setBounds(20,50,100,20);  
  10.         area=new TextArea();  
  11.         area.setBounds(20,80,300300);  
  12.         area.addKeyListener(this);  
  13.           
  14.         add(l);add(area);  
  15.         setSize(400,400);  
  16.         setLayout(null);  
  17.         setVisible(true);  
  18.     }  
  19.     public void keyPressed(KeyEvent e) {  
  20.         l.setText("Key Pressed");  
  21.     }  
  22.     public void keyReleased(KeyEvent e) {  
  23.         l.setText("Key Released");  
  24.     }  
  25.     public void keyTyped(KeyEvent e) {  
  26.         l.setText("Key Typed");  
  27.     }  
  28.   
  29.     public static void main(String[] args) {  
  30.         new KeyListenerExample();  
  31.     }  
  32. }  

Output:

java awt keylistener example 1

Java WindowListener Interface

The Java WindowListener is notified whenever you change the state of window. It is notified against WindowEvent. The WindowListener interface is found in java.awt.event package. It has three methods.

Methods of WindowListener interface

The signature of 7 methods found in WindowListener interface are given below:

  1. public abstract void windowActivated(WindowEvent e);  
  2. public abstract void windowClosed(WindowEvent e);  
  3. public abstract void windowClosing(WindowEvent e);  
  4. public abstract void windowDeactivated(WindowEvent e);  
  5. public abstract void windowDeiconified(WindowEvent e);  
  6. public abstract void windowIconified(WindowEvent e);  
  7. public abstract void windowOpened(WindowEvent e);  

Java WindowListener Example

  1. import java.awt.*;  
  2. import java.awt.event.WindowEvent;  
  3. import java.awt.event.WindowListener;  
  4. public class WindowExample extends Frame implements WindowListener{  
  5.     WindowExample(){  
  6.         addWindowListener(this);  
  7.           
  8.         setSize(400,400);  
  9.         setLayout(null);  
  10.         setVisible(true);  
  11.     }  
  12.       
  13. public static void main(String[] args) {  
  14.     new WindowExample();  
  15. }  
  16. public void windowActivated(WindowEvent arg0) {  
  17.     System.out.println("activated");  
  18. }  
  19. public void windowClosed(WindowEvent arg0) {  
  20.     System.out.println("closed");  
  21. }  
  22. public void windowClosing(WindowEvent arg0) {  
  23.     System.out.println("closing");  
  24.     dispose();  
  25. }  
  26. public void windowDeactivated(WindowEvent arg0) {  
  27.     System.out.println("deactivated");  
  28. }  
  29. public void windowDeiconified(WindowEvent arg0) {  
  30.     System.out.println("deiconified");  
  31. }  
  32. public void windowIconified(WindowEvent arg0) {  
  33.     System.out.println("iconified");  
  34. }  
  35. public void windowOpened(WindowEvent arg0) {  
  36.     System.out.println("opened");  
  37. }  
  38. }  

Output:

java awt windowlistener example 1 java awt windowlistener example 2
Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);   ...

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari...

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...