How to maximize a JFrame through code?
Asked
Active
Viewed 6.3k times
46
-
Did you try looking at the methods defined for JFrame and its super classes? – Andrew Thompson Mar 10 '11 at 10:28
-
4@Andrew: Yes sir, I did and it's too complicated for my ickle brains. – Tony Ogrewall Mar 10 '11 at 10:31
-
Heres a related [example](http://stackoverflow.com/questions/5207711). – trashgod Mar 10 '11 at 16:55
-
See also the answer I gave [here](https://stackoverflow.com/a/55592409/243373). – TT. Apr 09 '19 at 12:02
5 Answers
79
Try this:
f.setExtendedState( f.getExtendedState()|JFrame.MAXIMIZED_BOTH );

Harry Joy
- 58,650
- 30
- 162
- 207
-
I found the same after googling. However the above code does not work with newer versions of Java. – Tony Ogrewall Mar 10 '11 at 10:24
-
-
-
@Harry: Any alternative that will work for machines other than yours? – Tony Ogrewall Mar 10 '11 at 10:32
-
@Tony: are you setting `f.setResizable(false);`? if yes then it will not work. Show some of your code for better understanding. – Harry Joy Mar 10 '11 at 10:35
-
2@Harry: They have moved the constant `MAXIMIZED_WHATEVER` from `JFrame` to `java.awt.Frame`. That one change solved my problem. I suggest you update your answer as well. Thanks. – Tony Ogrewall Mar 10 '11 at 10:37
-
4@TonyOgrewall yes but afaik its inherited from `Frame` into `JFrame` so you can still use it. – Harry Joy Jan 03 '13 at 13:00
-
it work only after setVisible(true) in my case(windows 8.1). so i override setVisible function for showing JFrame with maximized state. – Fer Jan 22 '16 at 12:47
-
8
this works perfectly till java 7
public class TEST
{
public static void main(String args[])
{
JFrame jf= new JFrame();
jf.setVisible(true);
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
5
Yea the Toolkit solution ignores the windows task bar and uses the full screen which is not what you want.
For an immediate maximise of your form just add this in the JFrame
constructor after the InitiateComponents()
call.
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
The class extends JFrame
of course:
public class MyForm extends javax.swing.JFrame

Christoph
- 47,569
- 8
- 87
- 187

Swartblits
- 51
- 1
- 1
3
setExtendedState(JFrame.MAXIMIZED_BOTH); is not working is java 7
You can try this code it works.
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
setSize(xSize,ySize);

ArchuSu
- 31
- 1
-
1hmm ... MAXIMIZED_BOTH just works fine for me, in any recent version. What's your OS? BTW: the problem with manual sizing is that the taskbar isn't accounted for (at least not reliably) – kleopatra Sep 06 '13 at 07:55
-
1
for similar problems with: http://bugs.sun.com/view_bug.do?bug_id=7177173
An issue on jdk7. Try to call .setExtendedState() not directly after .setVisable()

Jan S
- 117
- 3
- 14