Java Swing GUI Application Full Screen on Linux

How to make your Java Swing application full screen on Linux

I have found this code on the stack overflow site:

static public boolean fullScreen(final JFrame frame, boolean doPack) {

  GraphicsDevice device = frame.getGraphicsConfiguration().getDevice();
  boolean result = device.isFullScreenSupported();

  if (result) {
    frame.setUndecorated(true);
    frame.setResizable(true);

    frame.addFocusListener(new FocusListener() {
      @Override
        public void focusGained(FocusEvent arg0) {
          frame.setAlwaysOnTop(true);
}

        @Override
        public void focusLost(FocusEvent arg0) {
          frame.setAlwaysOnTop(false);
        }
    });

    if (doPack)
      frame.pack();
      device.setFullScreenWindow(frame);
    } else {
      frame.setPreferredSize(
        frame.getGraphicsConfiguration().getBounds().getSize());


      if (doPack)
        frame.pack();

      frame.setResizable(true);

      frame.setExtendedState(Frame.MAXIMIZED_BOTH);
      boolean successful = frame.getExtendedState() == Frame.MAXIMIZED_BOTH;

      frame.setVisible(true);

      if (!successful)
          frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }
    return result;
}

This is important peace of code if you want to make a full screen application that works the same on both Linux and Windows OS.

For example, if you just write this:

setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);

... the code above would make a full screen on Windows, but on Linux you would still see the task bar:


The code that I found on the stack overflow makes a full screen on both Windows and Linux:


At the top of the screen, you can see burnt in pixels from the previous version of software which did not do the full screen (it had the task bar on it).


Comments

Comments powered by Disqus