32

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

cubanacan
  • 644
  • 1
  • 9
  • 26
Frank
  • 29,646
  • 56
  • 159
  • 233

6 Answers6

53

minimize with frame.setState(Frame.ICONIFIED)

restore with frame.setState(Frame.NORMAL)

Brad Mace
  • 26,397
  • 17
  • 98
  • 144
14

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((Window) frame); } finally { device.setFullScreenWindow(null); }

Refer to the JFrame documentation for more information.

Aaron Esau
  • 1,005
  • 3
  • 14
  • 30
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
  • 693
  • 9
  • 14
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
  • 141
  • 1
  • 8
-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
  • 7
    @kajacx Because other devs working on the project have no clue what `1` means; it's cryptic and harms readability, that's why – Dioxin Dec 24 '14 at 20:23
  • 6
    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