Advertisement



\
< Prev
Next >



WindowEvent and WindowListener





An event of type WindowEvent is generated in such situations -




Some methods of WindowEvent class


Method Description
public Window getWindow() Returns the window which triggered the WindowEvent.
public int getNewState() Returns the new state of the window.





A class to listen & respond to a WindowEvent must perform the next two steps:


Method Description
public void addWindowListener(ItemListener object) where object is an object of the class that has implemented Windowistener interface. Doing this, registers the class to listen and respond to WindowEvent.



Advertisement




Handling an WindowEvent by implementing WindowListener interface


In the upcoming code, we are going to create a class that will listen to WindowEvent, by implementing WindowListener interface. In this code, WindowEvent is generated when a key is pressed and released within a textfield.

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


public class WindowEx1 implements WindowListener
{
Label label1;
Frame frame;


WindowEx1()
{

frame = new Frame("Handling KeyEvent");

label1= new Label("-See window events -", Label.CENTER);

frame.setLayout(new FlowLayout());
frame.add(label1);

//Registering class WindowEx1 to catch and respond to window events 
frame.addWindowListener(this); 

frame.setSize(340,200);
frame.setVisible(true);
}


public void windowActivated(WindowEvent we)
{
System.out.println("Window Activated");
}

public void windowClosed(WindowEvent we)
{
System.out.println("Window Closed");
}

public void windowClosing(WindowEvent we)
{
frame.dispose();
System.out.println("Window Closing");
}

public void windowDeactivated(WindowEvent we)
{
System.out.println("Window Deactivated");
}

public void windowDeiconified(WindowEvent we)
{
System.out.println("Window Deiconified");
}

public void windowIconified(WindowEvent we)
{
System.out.println("Window Iconified/minimized");
}

public void windowOpened(WindowEvent e)
{
System.out.println("Window Opened for the first time");
}

public static void main(String... ar)
{
new WindowEx1();
}

}

When you run the code, you are presented a window shown in the window below -:

Figure 1


At the command prompt you are displayed two messages, due to the execution of methods in sequence:
Window Activated
Window Opened for the first time



When you click on the minimize button of this window to minimize it, the window is deactivated and deiconified.
Figure 2


Hence, two new messages are displayed on the command prompt, due to the execution of methods in sequence:
Window Iconified/minimized
Window Deactivated



When you bring up the minimized window, the window is reactivated.

Figure 3



Hence, a new message is displayed on the command prompt, due to the execution of the method:
Window Activated



When you click on the (x) button of this window to close it, the window is deactivated and closed.

Figure 4



Hence, three new messages are displayed on the command prompt, due to the execution of methods in sequence:
Window Closing
Window Deactivated
Window Closed




Please share this article -





< Prev
Next >
< ItemListener
AdjustmentListener >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement