awt

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.

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button