34

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

cubanacan
  • 644
  • 1
  • 9
  • 26
Frank
  • 30,590
  • 58
  • 161
  • 244

6 Answers6

55

minimize with frame.setState(Frame.ICONIFIED)

restore with frame.setState(Frame.NORMAL)

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
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.

Aaron Esau
  • 1,083
  • 3
  • 15
  • 31
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
Luke Chambers
  • 785
  • 1
  • 10
  • 16
1

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. – mjs Aug 25 '17 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);
Buddhi Kavindra
  • 171
  • 1
  • 9
-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);

  • 4
    `this.setState(1);` magic constants rules, why waste time writing `JFrame.ICONIFIED` when you can write `1` directly? :) – kajacx May 12 '14 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 Dec 24 '14 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 Dec 31 '14 at 16:08