JFrame Maximize window

JavaSwingMaximize Window

Java Problem Overview


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

Java Solutions


Solution 1 - Java

Provided that you are extending JFrame:

public void run() {
    MyFrame myFrame = new MyFrame();
    myFrame.setVisible(true);
    myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}

Solution 2 - Java

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);
    }
}

Solution 3 - Java

If you're using a JFrame, try this

JFrame frame = new JFrame();
//...
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

Solution 4 - Java

Solution 5 - Java

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);
	}
}

Solution 6 - Java

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);
	}
}

Solution 7 - Java

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);
				}
			}
		);
    }
}

Solution 8 - Java

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.

Solution 9 - Java

@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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNopeView Question on Stackoverflow
Solution 1 - JavakgiannakakisView Answer on Stackoverflow
Solution 2 - JavaVonCView Answer on Stackoverflow
Solution 3 - Javauser54579View Answer on Stackoverflow
Solution 4 - JavaZach ScrivenaView Answer on Stackoverflow
Solution 5 - JavaGreg ReynoldsView Answer on Stackoverflow
Solution 6 - Javaframe_max_showView Answer on Stackoverflow
Solution 7 - JavaTT.View Answer on Stackoverflow
Solution 8 - JavaWouterView Answer on Stackoverflow
Solution 9 - JavaAlexander ShkirkovView Answer on Stackoverflow