0

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

BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • 1
    [`invokeLater()`](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)? – Catalina Island Jun 27 '13 at 09:57
  • 2
    1) Java GUIs should be created and updated on the EDT. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. 2) See also [`Frame.setExtendedState(int)`](http://docs.oracle.com/javase/7/docs/api/java/awt/Frame.html#setExtendedState%28int%29). – Andrew Thompson Jun 27 '13 at 09:59
  • @AndrewThompson Yes, missed that, thanks! – BackSlash Jun 27 '13 at 10:01

2 Answers2

2

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();
            }
        });
    }
}
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
1

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 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7057287
To know more about this issue look at HERE.
UPDATE
As last tryI would suggest you to use JFrame#setAlwaysOnTop(true)

Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • Have you tried my update? You are referring to that bar which is shown in right hand side of screen? – Vishal K Jun 27 '13 at 10:26
  • THe right one and also the top one, i want a full screen undecorated window, so none of the bars should be visible while my application is in fullscreen mode – BackSlash Jun 27 '13 at 10:29
  • `GraphicsDevice#isFullScreenSupported()` is returning true in your application? – Vishal K Jun 27 '13 at 10:57
  • I'm using jdk1.7u25, by the way i'm not using KDE, i'm using Unity – BackSlash Jun 27 '13 at 11:16
  • Have you seen the link to stackoverflow that i sent you? It explained the reason..And in some answer it is also mentioned that it is not not working for unity also. – Vishal K Jun 27 '13 at 11:25