1

I'm having some issues getting my Java JFrame to be fullscreen on all OS (Windows, Mac, Linux). It seems whatever solution I find it does run on one OS but not on others or has some other serious bugs. I wanted to use the setFullScreenWindow(window w) method to properly initiate a fullscreen because setExtendedState(...) won't work on Mac/Linux as the menubar and taskbar are still visible.

The setFullScreenWindow(...) method worked fine on all environments until Java 7 came along and now there seems to be an issue that as soon as you enter fullscreen mode the application no longer responds to key events on Mac OS X. The application works just fine on Windows.

Does anyone have a clue how I could possibly work around this issue?

Note: The workaround described here (FullScreen Swing Components Fail to Receive Keyboard Input on Java 7 on Mac OS X Mountain Lion) does not work for Windows because it will result in the JFrame flickering and not opening properly.

The fullscreen approach described here is the same used below which does not work because of problems with the key input: (How to make a JFrame really fullscreen?)

Example Code:

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class FullScreenKeyTest extends JFrame {

public void createFrame() {
    initKey();
    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    this.setUndecorated(true);
    this.setVisible(true);
    gd.setFullScreenWindow(this);
}

private void initKey() {
    Action keyAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Escape key pressed");
            setVisible(false);
            System.exit(0);
        }
    };
    this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "keyPress");
    this.getRootPane().getActionMap().put("keyPress", keyAction);
}

public static void main(String[] args) {
    FullScreenKeyTest testFrame = new FullScreenKeyTest();
    testFrame.createFrame();
}
}
Community
  • 1
  • 1
jimmy
  • 4,471
  • 3
  • 22
  • 28

2 Answers2

1

This is a bit flaky as I can get it to work and break it at the same time.

With your example code, I added

getContentPane().setFocusable(true);
getContentPane().requestFocus();

To the end of the createFrame method, and instead of registering the action against the root pane, I registered against the content pane

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • By "flaky" you mean it's not a solution yet, right? I unfortunately can't test it right now because I don't have access to a Mac. What's the difference between registering it to contentPane or rootPane? – jimmy Jan 07 '13 at 10:27
  • Given your example, I could it to work. When I wrapped the contents of the main method in EventQueue.invokeLater, it stopped working - so no, I would not consider it reliable solution, but something that might get you moving in the right direction – MadProgrammer Jan 07 '13 at 10:44
  • The difference between registering the key bindings against the content pane as apposed to the root pane is the normally, the content pane resides within the root pane - I can also make the content pane focusable ;) – MadProgrammer Jan 07 '13 at 10:50
  • @jimmy I tried a number of different permutations and couldn't get it working reliably, this sounds like a bug to me – MadProgrammer Jan 07 '13 at 11:02
  • thanks for sharing your approach. I've opened up a bug with Oracle but I'm not very confident that it will be looked at... – jimmy Jan 07 '13 at 12:10
0

I found that keyboard inputs work if you use the native OS X Lion fullscreen API. See solution to the question Fullscreen feature for Java Apps on OSX Lion.

Community
  • 1
  • 1
Arthur Edelstein
  • 1,567
  • 1
  • 10
  • 11