Sunday, 6 April 2014

WindowListener


The listener interface for receiving window events.

public interface WindowListener extends EventListener {
    public void windowOpened(WindowEvent e);
    public void windowClosing(WindowEvent e);
    public void windowClosed(WindowEvent e);
    public void windowIconified(WindowEvent e);
    public void windowDeiconified(WindowEvent e);
    public void windowActivated(WindowEvent e);
    public void windowDeactivated(WindowEvent e);
}

void windowOpened(WindowEvent e)
Invoked the first time a window is made visible.

void windowClosing(WindowEvent e)
Invoked when the user attempts to close the window from the window's system menu.

void windowClosed(WindowEvent e)
Invoked when a window has been closed as the result of calling dispose on the window.

void windowIconified(WindowEvent e)
Invoked when a window is changed from a normal to a minimized state.

void windowDeiconified(WindowEvent e)
Invoked when a window is changed from a minimized to a normal state.

void windowActivated(WindowEvent e)
Invoked when the Window is set to be the active Window.

void windowDeactivated(WindowEvent e)
Invoked when a Window is no longer the active Window.


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class WindowListenerEx{
 JTextArea displayArea;
 TextField typingArea;
 JFrame frame1;
  
 WindowListenerEx(){
  
  /* Initialize typingArea */
  typingArea = new TextField(20);
  
  /* Initialize Display Area */
  displayArea = new JTextArea();
  displayArea.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(displayArea);
  scrollPane.setPreferredSize(new Dimension(150, 200));
   
  /* Initialize Frame */
  frame1 =new JFrame("Sample Frame");
  frame1.pack();
  frame1.setVisible(true);
  frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame1.getContentPane().add(typingArea, BorderLayout.PAGE_START);
  frame1.getContentPane().add(scrollPane, BorderLayout.CENTER);
  frame1.setSize(300, 500);
    
  frame1.addWindowListener(new WindowListener(){
   public void windowOpened(WindowEvent e){
    displayArea.append("Window Opened\n");
   } 
   public void windowClosing(WindowEvent e){
    System.out.println("Window is closing\n");
   } 
   public void windowIconified(WindowEvent e){
    displayArea.append("Window Iconified\n");
   } 
   public void windowDeiconified(WindowEvent e){
    displayArea.append("Window De Iconified\n");
   } 
   public void windowActivated(WindowEvent e){
    displayArea.append("Window Activated\n");
   } 
   public void windowClosed(WindowEvent e){
    System.out.println("Window Closed\n");
   } 
   public void windowDeactivated(WindowEvent e){
    displayArea.append("Window De Activated\n");
   } 
  });   
 }
 
 public static void main(String args[]){
  new WindowListenerEx();
 }
}

Output







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment