In my Java app, I have a JFrame window, how can I minimize it from my Java program ?
6 Answers
minimize with frame.setState(Frame.ICONIFIED)
restore with frame.setState(Frame.NORMAL)
-
@rogerdpack no, it works to show (visualize) hidden window, but not to restore from minimization Commented Sep 12, 2012 at 13:42
Minimize:
frame.setState(Frame.ICONIFIED);
Another way to minimize:
frame.setExtendedState(JFrame.ICONIFIED);
Normal size:
frame.setState(Frame.NORMAL);
Another way to normal size:
frame.setExtendedState(JFrame.NORMAL);
Maximize:
frame.setState(Frame.MAXIMIZED_BOTH);
Another way to maximize:
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Full Screen maximize:
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
try { device.setFullScreenWindow((java.awt.Window) frame); } catch (Exception e) { device.setFullScreenWindow(null); }
Refer to the JFrame
documentation for more information.
You can do this in two ways:
JFrame frame = new JFrame("Test");
frame.setExtendedState(JFrame.ICONIFIED); // One way
frame.setState(JFrame.ICONIFIED); // Another way
Another approach
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_ICONIFIED));
-
Didn't work. ICONIFIED did. Maybe i was firing the event to early, but the other one did work.– mjsCommented Aug 25, 2017 at 15:01
You can use following code:
this.setState(YourJFrame.ICONIFIED);
And you can use this code to maximize it:
this.setExtendedState(MAXIMIZED_BOTH);
If you are trying to code for a event of a component then try code below. And make sure the class which this code is included is extended by Frame class
private void closeMouseClicked(java.awt.event.MouseEvent evt){
this.setState(1);
}
Or create an instance of a Frame class and call setState(1);
-
4
this.setState(1);
magic constants rules, why waste time writingJFrame.ICONIFIED
when you can write1
directly? :)– kajacxCommented May 12, 2014 at 14:23 -
8@kajacx Because other devs working on the project have no clue what
1
means; it's cryptic and harms readability, that's why– VinceCommented Dec 24, 2014 at 20:23 -
7Besides, if they'd ever decide to change the value of the constants, your code would break for no apparent reason. Have fun debugging that– weeknieCommented Dec 31, 2014 at 16:08