How to detect JFrame window minimize and maximize events?
Asked Answered
C

3

8

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");

Chaechaeronea answered 22/6, 2012 at 1:3 Comment(2)
Yes, I need to know when the window is resize so that I can re-draw the components inside the frame.Chaechaeronea
The methods validate() and repaint()` will be called automatically when the frame is resized, but you may need to update other data structures; see also AnimationTest.Slattery
C
14

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

Codicodices answered 22/6, 2012 at 1:13 Comment(0)
I
7

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

For more details, visit this

Impedance answered 22/6, 2012 at 6:53 Comment(0)
A
7
  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");
   }
}
Attar answered 19/9, 2014 at 9:28 Comment(1)
is maximized means returned (showed) from minimized?Acidulant

© 2022 - 2024 — McMap. All rights reserved.