J_fruitty
J_fruitty

Reputation: 57

how to escape from full screen mode in java?

I have used this code to go full screen:

private void fullScreenPerformed(java.awt.event.ActionEvent evt) {

    full = new JFrame();
    full.setSize(Toolkit.getDefaultToolkit().getScreenSize());
    full.setUndecorated(true);
    full.setVisible(true);
}

When I run the program, the JFrame is stuck in the full-screen mode, and I can't close it when I press escape. Thus, I had to restart or log out of my computer to get back to normal screen again. I want the user to be able to close it by pressing the "escape button" or using other combinations. How can I do so?

Upvotes: 0

Views: 1900

Answers (2)

technikfischer
technikfischer

Reputation: 146

You need to add an keyEventListener for that, which disposes the frame if you have pressed escape. After that, it will not be usable again.

full.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent evt) {
        if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
            full.dispose();
    }
});

Keep in mind that the frame needs to be focused, or else the event is not fired.

Upvotes: 1

Kintu Barot
Kintu Barot

Reputation: 320

Try this

 KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
 int w = full.WHEN_IN_FOCUSED_WINDOW;
 dialog.getRootPane().registerKeyboardAction(e -> window.dispose(), k, w);

Upvotes: 0

Related Questions