5

I want to know how to make a java JFrame start out maximised. I don't want it to be fullscreen (no window around it) I just want it to start out like a normal program such as a web browser.

I already know about using:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

To get the screen size, but when you apply this Dimension to the JFrame it can still be dragged around even though it takes up almost all of the screen. You can press the maximse button to stop this but I would rather that the window started out maximised.

Also, I fear for the effects that maximising the window would have upon the contents of the window.

How do I go about doing this?

Jack O'Connor
  • 185
  • 1
  • 4
  • 11

1 Answers1

9

Use java.awt.Frame.setExtendedState():

public static void main(String[] args)  {

    JFrame frame = new JFrame();
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setVisible(true);
}
Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • Thanks very much! I have one question though, this may be stupid but why did you throw an InterruptedException? Thanks again! – Jack O'Connor Feb 14 '13 at 18:06
  • For no reason, sorry. I wrote this code in my editor in a main method that was already existing for some tests, and I forgot to fix the method signature before copy/paste here. – Cyrille Ka Feb 14 '13 at 18:13
  • Sorry I have another question. If someone restores the window down it becomes its normal size, which without using setSize is pretty much nothing. So I use setSize and now the window is resizable so I use setResizable(false) to change this. However, now the window doesnt start out maximised and cannot be maximised, how can i make give the window a size for when its not maximised, make the window start out maximised and make sure that it cant be resized when its restored down. Or make it start out maximised and make it impossible to restore it down. – Jack O'Connor Feb 14 '13 at 18:52
  • Can you ask a separate new question? it is hard to answer in comments. – Cyrille Ka Feb 14 '13 at 19:04
  • 1
    @Jako_Jako it sounds like 1- you don't have any components in your frame or 2- your not using a layout manager. Before you maximise the frame, try calling JFraem#pack. This should initialise the frame with a preferred size – MadProgrammer Feb 14 '13 at 19:12
  • Here is the new question. Thanks in advance. http://stackoverflow.com/questions/14882417/java-how-to-make-jframes-maximised-but-not-resizable – Jack O'Connor Feb 14 '13 at 19:20