Home » Desktop Java » awt » Determining when a frame is Iconized or Maximized or Opened or Closed

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Determining when a frame is Iconized or Maximized or Opened or Closed

With this example we are going to see how to determine when a frame is Iconized or Maximized or Opened or Closed. This is very useful when you have many windows working on you application and you want to know or even inform the user about the state of each window. Additionally you might your application to behave differently depending on the state of some windows

Basically to determine the state of the windows, one should follow these steps:

  • Create a new WindowAdapter instance.
  • Override windowStateChanged method to customize the handling of that specific event. This method will be called after a window state has been changed.
  • Use Frame.ICONIFIED, to determine when a window is iconified.
  • Override windowOpened method. This method is called after a window has been opened.
  • Override windowClosing method. This method is called when the user clicks the close button
  • Override windowClosed method. This method is called after a window is closed.

Let’s see the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;

public class FrameEventListener {

  public static void main(String[] args) {

// Create the frame

Frame frame = new Frame();

// Create a listener for window events

WindowAdapter listener = new WindowAdapter() {

    // This method is called after a window state has been changed

    public void windowStateChanged(WindowEvent evt) {

  Frame frame = (Frame)evt.getSource();

  int oldState = evt.getOldState();

  int newState = evt.getNewState();

  if ((oldState & Frame.ICONIFIED) == 0 && (newState & Frame.ICONIFIED) != 0) {

System.out.println("Frame was iconized");

  } else if ((oldState & Frame.ICONIFIED) != 0 && (newState & Frame.ICONIFIED) == 0) {

System.out.println("Frame was deiconized");

  }

  if ((oldState & Frame.MAXIMIZED_BOTH) == 0 && (newState & Frame.MAXIMIZED_BOTH) != 0) {

System.out.println("Frame was maximized");

  } else if ((oldState & Frame.MAXIMIZED_BOTH) != 0 && (newState & Frame.MAXIMIZED_BOTH) == 0) {

System.out.println("Frame was minimized");

  }

    }

    // This method is called after a window has been opened

    public void windowOpened(WindowEvent evt) {

  Frame frame = (Frame)evt.getSource();

  System.out.println("Window is opened");

    }

    // This method is called when the user clicks the close button

    public void windowClosing(WindowEvent evt) {

  Frame frame = (Frame)evt.getSource();

  System.out.println("Window is closing");

    }

    // This method is called after a window is closed

    public void windowClosed(WindowEvent evt) {

  Frame frame = (Frame)evt.getSource();

  System.out.println("Window closed");

    }

};

// Register the listeners with the frame

frame.addWindowStateListener((WindowStateListener)listener);

frame.addWindowListener((WindowListener)listener);

// Display the frame

int frameWidth = 300;

int frameHeight = 300;

frame.setSize(frameWidth, frameHeight);

frame.setVisible(true);

  }

}

 
This was an example on how to determining when a frame is Iconized or Maximized or Opened or Closed.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of