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

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

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

C++ (Object and Class)

The major purpose of C++ programming is to introduce the concept of object orientation to the C programming language. Object Oriented Programming is a paradigm that provides many concepts such as  inheritance, data binding, polymorphism etc. The programming paradigm where everything is represented as an object is known as truly object-oriented programming language.  Smalltalk  is considered as the first truly object-oriented programming language. OOPs (Object Oriented Programming System) Object  means a real word entity such as pen, chair, table etc.  Object-Oriented Programming  is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: Object Class Inheritance Polymorphism Abstraction Encapsulation C++ Object In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an ent...