Markel Mairs
Markel Mairs

Reputation: 741

How to detect JFrame window minimize and maximize events?

Is there a way to an event listener to a JFrame object to detect when the user clicks the window maximize or minimize buttons?

Am using the JFrame object as follows:

JFrame frame = new JFrame("Frame");

Upvotes: 8

Views: 28542

Answers (3)

agarwav
agarwav

Reputation: 400

Yes, you can do this by implementing WindowListener methods namely windowIconified(WindowEvent e) by windowDeiconified(WindowEvent e).

For more details, visit this

Upvotes: 7

pedromateo
pedromateo

Reputation: 3115

  1. Create a frame and add a listener:

JFrame frame = new JFrame();
frame.addWindowStateListener(new WindowStateListener() {
   public void windowStateChanged(WindowEvent arg0) {
      frame__windowStateChanged(arg0);
   }
});

  1. Implement the listener:

public void frame__windowStateChanged(WindowEvent e){
   // minimized
   if ((e.getNewState() & Frame.ICONIFIED) == Frame.ICONIFIED){
      _print("minimized");
   }
   // maximized
   else if ((e.getNewState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH){
      _print("maximized");
   }
}

Upvotes: 7

tenorsax
tenorsax

Reputation: 21223

You can use WindowStateListener. How to Write Window Listeners tutorial demonstrates how to create window-related event handlers.

Upvotes: 14

Related Questions