Java Swing How to - Create full screen JFrame








Question

We would like to know how to create full screen JFrame.

Answer

import java.awt.BorderLayout;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
//from w ww  .  j a v  a  2  s. com
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new BorderLayout());
    frame.add(new JLabel("Hit Escape to exit full screen", JLabel.CENTER),
        BorderLayout.CENTER);

    frame.setSize(300, 300);

    KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke("ESCAPE");
    Action escapeAction = new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        GraphicsEnvironment.getLocalGraphicsEnvironment()
            .getDefaultScreenDevice().setFullScreenWindow(null);
      }
    };

    frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
        .put(escapeKeyStroke, "ESCAPE");
    frame.getRootPane().getActionMap().put("ESCAPE", escapeAction);

    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
        .setFullScreenWindow(frame);

  }
}