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?

Edit: 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.

Edit: 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
  • A workaround is described here: http://stackoverflow.com/questions/13064607/fullscreen-swing-components-fail-to-receive-keyboard-input-on-java-7-on-mac-os-x – Werner Kvalem Vesterås Jan 06 '13 at 21:16
  • Unfortunately that workaround does not work on Windows. When opening the JFrame it will not stop flickering and won't display the window correctly. – jimmy Jan 06 '13 at 21:19
  • You could always detect what OS you are running on, and select the correct strategy accordingly. – Werner Kvalem Vesterås Jan 06 '13 at 21:22
  • I was hoping for a nicer solution where I wouldn't have to query different OS properties. But how can you get fullscreen to work on Mac OS X if not by using setFullScreenWindow()? As stated the setExtendedState method is not a proper fullscreen. – jimmy Jan 06 '13 at 21:32

0 Answers0