BackSlash
BackSlash

Reputation: 22243

Full screen frame issue

I have this code, which basically initializes a new JFrame and sets it full screen

public class FullScreenFrameTest extends JFrame {

    public FullScreenFrameTest() {
        super();
        initFrame();
        setVisible(true);

        //full screen
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();
        device.setFullScreenWindow(this);
        //end full screen
    }

    public void initFrame() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(true);
        setLocation(0, 0); //tried removing this, still doesn't work
        setSize(screen.width, screen.height);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
        new FullScreenFrameTest();
    }
}

The problem is that it sometimes it does work and sometimes it doesn't, especially with Ubuntu: sometimes i see it full screen, sometimes the two bars are shown. What am i missing?

UPDATE

There is a screenshot:

Screenshot

Upvotes: 0

Views: 1550

Answers (2)

Vishal K
Vishal K

Reputation: 13066

Change

setLocation(0, 0); //tried removing this, still doesn't work
setSize(screen.width, screen.height);

To

setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
setLocationRelativeTo(null);

And call FullScreenFrameTest() constructor within SwingUtilities.invokeLater.

UPDATE

This might be due to the bug in java run time environment. Here is the bug reported https://bugs.java.com/bugdatabase/view_bug?bug_id=7057287
To know more about this issue look at HERE.
UPDATE
As last tryI would suggest you to use JFrame#setAlwaysOnTop(true)

Upvotes: 1

Catalina Island
Catalina Island

Reputation: 7136

Make sure to build your GUI on the event dispatch thread with invokeLater().

Update: Here's an SSCCE that seems to work consistently.

import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FullScreenFrameTest extends JFrame {

    public FullScreenFrameTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(true);
        add(new JLabel("Test", JLabel.CENTER));
        GraphicsEnvironment env =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();
        device.setFullScreenWindow(this);
        setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FullScreenFrameTest();
            }
        });
    }
}

Upvotes: 2

Related Questions