JFrame in full screen Java

308,879

Solution 1

Add:

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

Solution 2

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

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

Solution 4

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

Easiest fix ever:

for ( Window w : Window.getWindows() ) {
    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow( w );
}
Share:
308,879
Yoda
Author by

Yoda

If you have a question about Bonjour in .NET and AXIS SDK I am the guy. I HATE telerik.

Updated on February 10, 2021

Comments

  • Yoda
    Yoda over 3 years

    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?
    
  • shaILU
    shaILU over 6 years
  • Admin
    Admin about 4 years
  • Kelvin Bouma
    Kelvin Bouma over 2 years
    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.
  • Slav
    Slav about 2 years
    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.