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,100, 50,50);    
  13.         checkBox2 = new Checkbox("Java");    
  14.         checkBox2.setBounds(100,150, 50,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,300, 300);  
  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

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...

Genotype Representation

Binary Representation This is one of the simplest and most widely used representation in GAs. In this type of representation the genotype consists of bit strings. For some problems when the solution space consists of Boolean decision variables – yes or no, the binary representation is natural. Take for example the 0/1 Knapsack Problem. If there are n items, we can represent a solution by a binary string of n elements, where the x th  element tells whether the item x is picked (1) or not (0). For other problems, specifically those dealing with numbers, we can represent the numbers with their binary representation. The problem with this kind of encoding is that different bits have different significance and therefore mutation and crossover operators can have undesired consequences. This can be resolved to some extent by using  Gray Coding,  as a change in one bit does not have a massive effect on the solution. Real Valued Representation For problems where we want to define ...

Data Warehousing - An Overview

Anurag Information Technology (IT) has historically influenced organizational performance and competitive standing. The increasing processing power and sophistication of analytical tools and techniques have put the strong foundation for the product called data warehouse. There are a number of reasons that any organization should consider a data warehouse, which can be the critical tool for maximizing the organization’s investment in the information it has collected and stored throughout the enterprise. IT managers need to understand the rationale and benefits of data warehouses because they may need to design and implement, or procure this kingpin of business intelligence. The data warehouses are supposed to provide storage, functionality and responsiveness to queries beyond the capabilities of today's transaction-oriented databases. Also data warehouses are set to improve the data access performance of databases. Traditional databases balance the requirement of data access w...