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

mKorbel
  • 109,107
  • 18
  • 130
  • 305
Markel Mairs
  • 742
  • 2
  • 10
  • 28
  • Yes, I need to know when the window is resize so that I can re-draw the components inside the frame. – Markel Mairs Jun 22 '12 at 01:40
  • 2
    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`](http://stackoverflow.com/a/3256941/230513). – trashgod Jun 22 '12 at 01:57

3 Answers3

14

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

tenorsax
  • 20,833
  • 9
  • 58
  • 105
7

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

For more details, visit this

Ahmed Ashour
  • 4,462
  • 10
  • 33
  • 49
agarwav
  • 400
  • 2
  • 7
  • 16
6
  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");
   }
}
pedromateo
  • 2,735
  • 1
  • 17
  • 19