134

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?
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
Yoda
  • 17,363
  • 67
  • 204
  • 344

14 Answers14

243

Add:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
frame.setUndecorated(true);
frame.setVisible(true);
Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Done, https://stackoverflow.com/questions/46804305/jframe-in-full-screen-without-undercorated – shaILU Oct 18 '17 at 06:51
  • See: https://stackoverflow.com/questions/62286865/intstream-iterate-function-with-filter#comment110160167_62286865 –  Jun 09 '20 at 16:41
  • 4
    I do not understand at all why this is being upvoted. It is the incorrect way to do this and will not even work properly on some versions of Linux. It is not fullscreen, it is a maximized window without decorations (which is not the same thing!). The correct answer is the one by Paul Vargas, not this one. – Kelvin Bouma Mar 04 '22 at 18:05
  • 1
    This answer described so called "windowed fullscreen" mode. The answer by Paul Vargas describes "exclusive fullscreen" mode. Both can be useful, depending on the situation. – Slav Apr 14 '22 at 14:47
  • This solution actually tend to work even better than Paul's solution when the user has 2 monitors. For example I want to make a small tool which displays a copy of your main monitor with some overlayed information on your secondary monitor. On Windows Paul's solution results in the copy only being visible when you're tabbed into the Java app, making the first monitor and everything on it unusable. When clicking e.g. a text field on your main monitor the screen briefly goes black, the Java app disappears and stops showing your overlayed info until you tab in it again, while this solution works. – Nico Wawrzyniak Dec 24 '22 at 05:02
52

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

}
Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • 1
    There is a problem with this method; it works perfectly on my home PC with a separate monitor, but at my work office my computer is an all-in-one and it simply makes a maximized window fill my screen. – Blue Dev Feb 24 '23 at 23:15
12

Use setExtendedState(int state), where state would be JFrame.MAXIMIZED_BOTH.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
8

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.

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Chan
  • 2,601
  • 6
  • 28
  • 45
7

Easiest fix ever:

for ( Window w : Window.getWindows() ) {
    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow( w );
}
jrswgtr
  • 2,287
  • 8
  • 23
  • 49
Ariel Terrani
  • 618
  • 6
  • 6
5

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.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
PeGiannOS
  • 227
  • 4
  • 19
  • Oof, no. 1, Please don't reply with a massive block of code for the whole class that includes no explanation. 2, don't use underscores in variable names, just use camelcase for variables and pascalcase for classes. – Blue Dev Feb 24 '23 at 23:19
3

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.

Kyon Perez
  • 124
  • 1
  • 13
2

it will helps you use your object instant of the frame

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

Jerin Stephen
  • 71
  • 2
  • 9
2

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();
cmaher
  • 5,100
  • 1
  • 22
  • 34
Saad K
  • 21
  • 4
2
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));

This just makes the frame the size of the screen

Sly
  • 21
  • 1
2

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

extendedState 6
resizeable    true
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
1

Values highlighted that I mean:

JFrame - Properties

sanyassh
  • 8,100
  • 13
  • 36
  • 70
Rudi Wijaya
  • 872
  • 1
  • 10
  • 25
0

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);
        }
    }
Aarush Kumar
  • 149
  • 1
  • 8
-1

Set 2 properties below:

  1. extendedState = 6
  2. resizeable = true

It works for me.

Rudi Wijaya
  • 872
  • 1
  • 10
  • 25