JFrame in full screen Java

JavaSwingFullscreen

Java Problem Overview


I will be doing a project soon and I will have to use full screen mode in it.

It will draw some graphics in the window. It would be convienient if I use JFrame or something similar.

I don't know what the final resolution of the screen will be. Please tell me if the graphics will be automaticly rescaled?

JFrame jf = new JFrame();
jf.setSize(1650,1080);
//make it fullscreen;
//now is everything is going to be rescaled so it looks like the original?

Java Solutions


Solution 1 - Java

Add:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
frame.setUndecorated(true);
frame.setVisible(true);

Solution 2 - Java

If you want put your frame in full-screen mode (like a movie in full-screen), check these answers.

The classes java.awt.GraphicsEnvironment and java.awt.GraphicsDevice are used for put an app in full-screen mode on the one screen (the dispositive).

e.g.:

static GraphicsDevice device = GraphicsEnvironment
		.getLocalGraphicsEnvironment().getScreenDevices()[0];

public static void main(String[] args) {

	final JFrame frame = new JFrame("Display Mode");
	frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setUndecorated(true);

	JButton btn1 = new JButton("Full-Screen");
	btn1.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			device.setFullScreenWindow(frame);
		}
	});
	JButton btn2 = new JButton("Normal");
	btn2.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			device.setFullScreenWindow(null);
		}
	});

	JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
	panel.add(btn1);
	panel.add(btn2);
	frame.add(panel);

	frame.pack();
	frame.setVisible(true);

}

Solution 3 - Java

Solution 4 - Java

One way is to use the Extended State. This asks the underlying OS to maximize the JFrame.

setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

Other approach would be to manually maximize the screen for you requirement.

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(100, 100, (int) dim.getWidth(), (int) dim.getHeight());
setLocationRelativeTo(null);

But this has pitfalls in Ubuntu OS. The work around I found was this.

if (SystemHelper.isUnix()) {
    getContentPane().setPreferredSize(
	Toolkit.getDefaultToolkit().getScreenSize());
	pack();
	setResizable(false);
	show();

	SwingUtilities.invokeLater(new Runnable() {
	    public void run() {
		    Point p = new Point(0, 0);
			SwingUtilities.convertPointToScreen(p, getContentPane());
			Point l = getLocation();
			l.x -= p.x;
			l.y -= p.y;
			setLocation(p);
        }
    });
}

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(100, 100, (int) dim.getWidth(), (int) dim.getHeight());
setLocationRelativeTo(null);

In Fedora the above problem is not present. But there are complications involved with Gnome or KDE. So better be careful. Hope this helps.

Solution 5 - Java

Easiest fix ever:

for ( Window w : Window.getWindows() ) {
    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow( w );
}

Solution 6 - Java

Just use this code :

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

public class FullscreenJFrame extends JFrame {
	private JPanel contentPane = new JPanel();
	private JButton fullscreenButton = new JButton("Fullscreen Mode");
	private boolean Am_I_In_FullScreen = false;
	private int PrevX, PrevY, PrevWidth, PrevHeight;

	public static void main(String[] args) {
		FullscreenJFrame frame = new FullscreenJFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 500);
		frame.setVisible(true);
	}

	public FullscreenJFrame() {
		super("My FullscreenJFrame");

		setContentPane(contentPane);
		
		// From Here starts the trick
		FullScreenEffect effect = new FullScreenEffect();

		fullscreenButton.addActionListener(effect);

		contentPane.add(fullscreenButton);
		fullscreenButton.setVisible(true);
	}

	private class FullScreenEffect implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (Am_I_In_FullScreen == false) {
				PrevX = getX();
				PrevY = getY();
				PrevWidth = getWidth();
				PrevHeight = getHeight();

				// Destroys the whole JFrame but keeps organized every Component
				// Needed if you want to use Undecorated JFrame dispose() is the
				// reason that this trick doesn't work with videos.
				dispose();
				setUndecorated(true);

				setBounds(0, 0, getToolkit().getScreenSize().width,
						getToolkit().getScreenSize().height);
				setVisible(true);
				Am_I_In_FullScreen = true;
			} else {
				setVisible(true);
				setBounds(PrevX, PrevY, PrevWidth, PrevHeight);
				dispose();
				setUndecorated(false);
				setVisible(true);
				Am_I_In_FullScreen = false;
			}
		}
	}
}

I hope this helps.

Solution 7 - Java

it will helps you use your object instant of the frame

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

Solution 8 - Java

First of all, that is the resolution you would want to use, 1650,1080.

Now add:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

If you have issues with the components on the JFrame, then after you have added all the components using the frame.add(component) method, add the following statement.

frame.pack();

Solution 9 - Java

You only need this:

JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

When you use the MAXIMIZED_BOTH modifier, it will max all the way across the window (height and width).

There are some that suggested using this:

frame.setUndecorated(true);

I won't recommend it, because your window won't have a header, thus no close/restore/minimize button.

Solution 10 - Java

JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));

This just makes the frame the size of the screen

Solution 11 - Java

You can use properties tool.
Set 2 properties for your JFrame:

extendedState 6
resizeable    true

Solution 12 - Java

Values highlighted that I mean:

JFrame - Properties

Solution 13 - Java

you can simply do like this -

public void FullScreen() {
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            final View v = this.activity.getWindow().getDecorView();
            v.setSystemUiVisibility(8);
        }
        else if (Build.VERSION.SDK_INT >= 19) {
            final View decorView = this.activity.getWindow().getDecorView();
            final int uiOptions = 4102;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

Solution 14 - Java

Set 2 properties below:

  1. extendedState = 6
  2. resizeable = true

It works for me.

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
QuestionYodaView Question on Stackoverflow
Solution 1 - JavaReimeusView Answer on Stackoverflow
Solution 2 - JavaPaul VargasView Answer on Stackoverflow
Solution 3 - JavaEng.FouadView Answer on Stackoverflow
Solution 4 - JavaChanView Answer on Stackoverflow
Solution 5 - JavaAriel TerraniView Answer on Stackoverflow
Solution 6 - JavaPeGiannOSView Answer on Stackoverflow
Solution 7 - JavaJerin StephenView Answer on Stackoverflow
Solution 8 - JavaSaad KView Answer on Stackoverflow
Solution 9 - JavaKyon PerezView Answer on Stackoverflow
Solution 10 - JavaSlyView Answer on Stackoverflow
Solution 11 - JavaBurhanYaprakView Answer on Stackoverflow
Solution 12 - JavaRudi WijayaView Answer on Stackoverflow
Solution 13 - JavaAarush KumarView Answer on Stackoverflow
Solution 14 - JavaRudi WijayaView Answer on Stackoverflow