Maximizing windows in Java Swing

JFrame frame = new JFrame();

// add all components

frame.pack();

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

frame.setVisible(true);

The key here is that you must call setExtendedState after any pack() methods, else it has no effect and you’ll spend two hours searching google and cursing Swing…trust me on this.

2007.05.22