96

I'm putting together a quick and dirty animation using swing. I would like the window to be maximized. How can I do that?

Sorter
  • 9,704
  • 6
  • 64
  • 74
Nope
  • 34,682
  • 42
  • 94
  • 119

9 Answers9

144

Provided that you are extending JFrame:

public void run() {
    MyFrame myFrame = new MyFrame();
    myFrame.setVisible(true);
    myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • 15
    It is a bad practice to access static field through an instance. Use `JFrame.MAXIMIZED_BOTH` instead. – nimcap Dec 15 '10 at 08:33
  • 3
    Huge, massive bug and issues with the above! (Well, okay fine, minor warnings with no major consequence...) Should be Frame.MAXIMIZED_BOTH not JFrame.MAXIMIZED_BOTH! :) – Manius May 16 '11 at 00:47
  • Why do you call setVisible(true) first? Is it significant? It also works fine without (ubuntu). – AvrDragon Sep 26 '12 at 11:25
  • 1
    Without `setVisible(true)` first, the frame is not sized correctly. I tried it both before and after `setExtendedState()`, and `setVisible()` must be first. – mjibson Dec 16 '12 at 04:27
  • 3
    @Crusader Absolutely no issue here actually, since both refer to the same constant field (`JFrame` inherits from `Frame`). – Guillaume Polet Jun 21 '13 at 07:54
  • @AvrDragon After experimentation, I discovered that it must the the LAST frame.setVisible(). For example, if I already have a frame.setVisible(), just plonking another one before the maximizing line of code for the purposes of making it work won't work. – Joehot200 Sep 11 '14 at 11:35
  • on mac os x this will create a strange maximizing-animation. you can prevent it by ```calling myFrame.setSize( myFrame.getGraphicsConfiguration().getBounds().getSize() );``` before making the frame visible. – kritzikratzi Feb 27 '15 at 09:35
  • @Crusader Good catch -- calling from Scala it makes a difference. – Owen Mar 10 '16 at 21:44
  • 2
    You are not required to extend `JFrame` to use the above code example. In fact, it's not generally a recommended approach - use composition instead. This code would work perfectly fine on a private `JFrame` instance. – Duncan Jones Aug 11 '16 at 08:09
  • @mjibson +1 - Thank you! Helped me to get rid of visual artifacts from incorrect drawing of the lower and right side of the frame in maximaized state. – PhilipWalker Jul 17 '20 at 21:01
18

Something like this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame
{
    public Test()
    {
        GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
        this.setMaximizedBounds(env.getMaximumWindowBounds());
        this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
    }

    public static void main(String[] args)
    {
        JFrame.setDefaultLookAndFeelDecorated(true);

        Test t = new Test();
        t.setVisible(true);
    }
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • What is the `|` for? Does it do one, and if that fails it does the other? Or are you indicating programmer choice (I.e. pick one of A|B to go in this call?) I've not seen this syntax before. – AncientSwordRage Feb 27 '13 at 15:58
  • @Pureferret it is the "bitwise inclusive OR" operator, which copies a bit if it exists in either operand. (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html or http://www.tutorialspoint.com/java/java_basic_operators.htm) – VonC Feb 27 '13 at 16:11
16

If you're using a JFrame, try this

JFrame frame = new JFrame();
//...
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
TT.
  • 15,774
  • 6
  • 47
  • 88
user54579
  • 4,588
  • 4
  • 23
  • 19
7

How about JFrame.setExtendedState(JFrame.MAXIMIZED_BOTH)?

Zach Scrivena
  • 29,073
  • 11
  • 63
  • 73
2

i like this version:

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Test
{
    public static void main(String [] args)
    {
        final JFrame frame = new JFrame();
        final GraphicsConfiguration config = frame.getGraphicsConfiguration();

        final int left = Toolkit.getDefaultToolkit().getScreenInsets(config).left;
        final int right = Toolkit.getDefaultToolkit().getScreenInsets(config).right;
        final int top = Toolkit.getDefaultToolkit().getScreenInsets(config).top;
        final int bottom = Toolkit.getDefaultToolkit().getScreenInsets(config).bottom;

        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        final int width = screenSize.width - left - right;
        final int height = screenSize.height - top - bottom;

        frame.setResizable(false);
        frame.setSize(width,height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.setVisible(true);
    }
}
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
2

The way to set JFrame to full-screen, is to set MAXIMIZED_BOTH option which stands for MAXIMIZED_VERT | MAXIMIZED_HORIZ, which respectively set the frame to maximize vertically and horizontally

package Example;
import java.awt.GraphicsConfiguration;
import javax.swing.JFrame;
import javax.swing.JButton;

public class JFrameExample
{
    static JFrame frame;
    static GraphicsConfiguration gc;
    public static void main(String[] args)
    {
        frame = new JFrame(gc);
        frame.setTitle("Full Screen Example");
        frame.setExtendedState(MAXIMIZED_BOTH);

        JButton button = new JButton("exit");
        b.addActionListener(new ActionListener(){@Override
        public void actionPerformed(ActionEvent arg0){
            JFrameExample.frame.dispose();
            System.exit(0);
        }});

        frame.add(button);
        frame.setVisible(true);
    }
}
Khaled.K
  • 5,828
  • 1
  • 33
  • 51
Greg Reynolds
  • 9,736
  • 13
  • 49
  • 60
2

I've tried the solutions in this thread and the ones here, but simply calling setExtendedState(getExtendedState()|Frame.MAXIMIZED_BOTH); right after calling setVisible(true); apparently does not work for my environment (Windows 10, JDK 1.8, my taskbar is on the right side of my screen). Doing it this way still leaves a tiny space on the left, right and bottom .

What did work for me however, is calling setExtendedState(... when the window is activated, like so:

public class SomeFrame extends JFrame {

    public SomeFrame() {
        // ...
        setVisible(true);
        setResizable(true);
        // if you are calling setSize() for fallback size, do that here
        addWindowListener (
            new WindowAdapter() {
                private boolean shown = false;
                @Override
                public void windowActivated(WindowEvent we) {
                    if(shown) return;
                    shown = true;
                    setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
                }
            }
        );
    }
}
TT.
  • 15,774
  • 6
  • 47
  • 88
  • 1
    I had a similar issue on Windows 10 with the taskbar on the bottom of the screen. The window was too high and therefore overlapped by the taskbar. Your solution worked! – ConveniencePatterns May 09 '20 at 11:38
0

I ended up using this code:

public void setMaximized(boolean maximized){
    if(maximized){
        DisplayMode mode = this.getGraphicsConfiguration().getDevice().getDisplayMode();
        Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
        this.setMaximizedBounds(new Rectangle(
                mode.getWidth() - insets.right - insets.left, 
                mode.getHeight() - insets.top - insets.bottom
        ));
        this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
    }else{
        this.setExtendedState(JFrame.NORMAL);
    }
}

This options worked the best of all the options, including multiple monitor support. The only flaw this has is that the taskbar offset is used on all monitors is some configurations.

Wouter
  • 538
  • 6
  • 15
0

@kgiannakakis answer is fully correct, but if someone stuck into this problem and uses Java 6 on Linux (by example, Mint 19 Cinnamon), MAXIMIZED_BOTH state is sometimes not applied.

You could try to call pack() method after setting this state.

Code example:

public MainFrame() {
    setContentPane(contentPanel); //some JPanel is here
    setPreferredSize(new Dimension(1200, 800));
    setMinimumSize(new Dimension(1200, 800));
    setSize(new Dimension(1200, 800));
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    pack();
}

This is not necessary if you are using Java 7+ or Java 6 on Windows.

Alexander Shkirkov
  • 3,527
  • 3
  • 17
  • 37