35

In my Java app, I have a JFrame window, how can I minimize it from my Java program ?

6 Answers 6

57

minimize with frame.setState(Frame.ICONIFIED)

restore with frame.setState(Frame.NORMAL)

1
  • @rogerdpack no, it works to show (visualize) hidden window, but not to restore from minimization
    – cubanacan
    Commented Sep 12, 2012 at 13:42
17

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.

12

You can do this in two ways:

JFrame frame = new JFrame("Test");

frame.setExtendedState(JFrame.ICONIFIED); // One way
frame.setState(JFrame.ICONIFIED); // Another way
1

Another approach

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_ICONIFIED));
1
  • Didn't work. ICONIFIED did. Maybe i was firing the event to early, but the other one did work.
    – mjs
    Commented Aug 25, 2017 at 15:01
0

You can use following code:

this.setState(YourJFrame.ICONIFIED);

And you can use this code to maximize it:

this.setExtendedState(MAXIMIZED_BOTH);
-1

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

3
  • 4
    this.setState(1); magic constants rules, why waste time writing JFrame.ICONIFIED when you can write 1 directly? :)
    – kajacx
    Commented 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
    – Vince
    Commented Dec 24, 2014 at 20:23
  • 7
    Besides, if they'd ever decide to change the value of the constants, your code would break for no apparent reason. Have fun debugging that
    – weeknie
    Commented Dec 31, 2014 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.