How to define a JFrame window to minimize and maximize events?

Is there an event listener way to a JFrame object to detect when a user clicks on a window, maximizes or collapses the buttons?

We use the JFrame object as follows:

JFrame frame = new JFrame("Frame");

+6
source share
3 answers

You can use WindowStateListener . How to write Window Listeners in a tutorial shows how to create event handlers associated with a window.

+11
source

Yes, you can do this by implementing WindowListener methods, namely windowIconified (WindowEvent e), with windowDeiconified (WindowEvent e). For more information visit this

+4
source
  • Create a frame and add a listener:

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

 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"); } } 
+4
source

Source: https://habr.com/ru/post/918713/


All Articles