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

locking

DBMS Locking Part I (DBMS only) TECHNICAL ARTICLES -> PERFORMANCE ARTICLES [  Back  ] [  Next  ] DBMS is often criticized for excessive locking – resulting in poor database performance when sharing data among multiple concurrent processes. Is this criticism justified, or is DBMS being unfairly blamed for application design and implementation shortfalls? To evaluate this question, we need to understand more about DBMS locking protocols. In this article, we examine how, why, what and when DBMS locks and unlocks database resources. Future articles will address how to minimize the impact of database locking. THE NEED FOR LOCKING In an ideal concurrent environment, many processes can simultaneously access data in a DBMS database, each having the appearance that they have exclusive access to the database. In practice, this environment is closely approximated by careful use of locking protocols. Locking is necessary in a concurrent environment to as...

DATA WAREHOUSE VERSUS DATA MART: THE GREAT DEBATE

DATA WAREHOUSE VERSUS DATA MART: THE GREAT DEBATE Customers exploring the field of business intelligence for the first time often lead with: What is the difference between a data warehouse and a data mart? The next question follows as predictably as night follows day: which one does my company need? Let me start by saying that the two terms are often confused. Indeed, some people in the industry use them virtually interchangeably, which is unfortunate, because they do reflect a valuable hierarchical difference. The Data Warehouse A "data warehouse" will typically contain the full range of business intelligence available to a company from all sources. That data consists of transaction-processing records, corporate and marketing data, and other business operations information; for example, a bank might include loans, credit card statements, and demand deposits data, along with basic customer information. This internal data is frequently combined with statistica...