How can I do full screen in Java on OSX
Asked Answered
G

3

11

I've been trying and failing to use the java full screen mode on the primary display of an OSX system. Whatever I've tried I can't seem to get rid of the 'apple' menu bar from the top of the display. I really need to paint over the entire screen. Can anyone tell me how to get rid of the menu?

I've attached an example class which exhibits the problem - on my system the menu is still visible where I would expect to see a completely blank screen.

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

public class FullScreenFrame extends JFrame implements KeyListener {

    public FullScreenFrame () {
        addKeyListener(this);
        setUndecorated(true);
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

        if (gd.isFullScreenSupported()) {
            try {
                gd.setFullScreenWindow(this);
            }
            finally {
                gd.setFullScreenWindow(null);
            }
        }
        else {
            System.err.println("Full screen not supported");
        }

        setVisible(true);
    }

    public void keyTyped(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {
        setVisible(false);
        dispose();
    }

    public static void main (String [] args) {
        new FullScreenFrame();
    }
}
Genteelism answered 20/7, 2009 at 20:39 Comment(2)
Why are you making your window full screen and then immediately calling setFullScreenWindow(null)?Prandial
@mmyers: That's the answer. Please add is as such, I can't resist the temptationGarlandgarlanda
P
13

I think your problem is here:

try {
        gd.setFullScreenWindow(this);
}
finally {
        gd.setFullScreenWindow(null);
}

finally blocks are always executed, so what happens here is that you window becomes full screen for a brief instant (if that) and then relinquishes the screen immediately.

Also, setVisible(true) is not necessary when you have previously called setFullScreenWindow(this), according to the Javadocs.

So I would change the constructor to this:

public FullScreenFrame() {
    addKeyListener(this);

    GraphicsDevice gd =
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    if (gd.isFullScreenSupported()) {
        setUndecorated(true);
        gd.setFullScreenWindow(this);
    } else {
        System.err.println("Full screen not supported");
        setSize(100, 100); // just something to let you see the window
        setVisible(true);
    }
}
Prandial answered 20/7, 2009 at 21:5 Comment(3)
Thanks - that works! The try - finally construct was taken from java.sun.com/docs/books/tutorial/extra/fullscreen/… where it suggests this form to prevent the application from keeping the screen after exiting - but it seems I'll have to actively keep the lock on the screen to prevent it being released too early. Thanks!Genteelism
Notice the "..." in there? That's where something that blocks until the window is closed should go. The try...finally approach simply guards against exceptions which might keep your application from releasing the screen when it's done. (Oddly, even though setFullScreenWindow makes the window visible, it doesn't block like setVisible does. I wonder if that is by design.)Prandial
Now does the graphic device act like a jframe ie you can add jpannels to it?Etherize
R
13

On OS X (10.7 and higher), it is better to use the native fullscreen mode available. You should use:

com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(window,true);
com.apple.eawt.Application.getApplication().requestToggleFullScreen(window);

where window is the window (JFrame, etc) that you want to take fullscreen

Ruprecht answered 15/11, 2013 at 3:57 Comment(4)
Wonderful! I needed this because, at least on OS X, menu accelerators do not work in the typical graphics fullscreen mode. This is the real OS X solution +1Cantus
You would need reflection to test for those classes and run the code right?Lucier
@UnitedStatesOfAmerica Reflection is one way. Alternatively, you could use a stub jar on non-apple platforms, like is provided here: stackoverflow.com/a/2639395Ruprecht
Perfect. This is absolutely what's needed for OSXInefficacy
S
0

Thats a bit pedantic, the answer is to follow the tutorial completely, which has the essentials and is somewhat more expansive than would fit in a post. The above sample does not work because it is missing a validate(); and some content. I suspect the Java Tutorial will not disappear any time soon. Below is a modified version

package test;

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

public class FullScreenFrame extends JFrame implements KeyListener {

    public FullScreenFrame () {
        addKeyListener(this);
        setUndecorated(true);
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    if (gd.isFullScreenSupported()) {
        try {
            this.getContentPane().addKeyListener(this);
            this.getContentPane().setLayout(new BorderLayout());
            this.getContentPane().add("Center", new JLabel("Full Screen, back to normal in 10 seconds"));
            gd.setFullScreenWindow(this);
            validate();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } finally {
            gd.setFullScreenWindow(null);
        }
    } else {
        System.err.println("Full screen not supported");
    }

}

public void keyTyped(KeyEvent e) {
    System.out.println("keyTyped:" +  e.getKeyChar() + "source:"  + e.getSource() );
}
public void keyPressed(KeyEvent e) {
    System.out.println("keyPressed:" + e.getKeyChar() + "source:"  + e.getSource() );
}
public void keyReleased(KeyEvent e) {
    System.out.println("keyReleased:" + e.getKeyChar() + "source:"  + e.getSource() );
       setVisible(false);
        dispose();
    }

    public static void main (String [] args) {
        new FullScreenFrame();
    }
}
Supersonic answered 7/5, 2018 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.