-2

I've built a swing window in eclipse that goes to fullscreen when opened, however it becomes impossible to toggle the fullscreen off.

I've tried researching online (I am aware of similar threads) but I've come across issues when attempting solutions.

Here is my JFrame

    private void initialize()
    {
    frmCpStats = new JFrame();
    frmCpStats.setTitle("CP Stats");
    Toolkit tk = Toolkit.getDefaultToolkit();
    int x =((int) tk.getScreenSize().getWidth());
    int y =((int) tk.getScreenSize().getHeight());
    frmCpStats.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frmCpStats.setSize(x, y);
    frmCpStats.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmCpStats.setUndecorated(true);
     KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
     int w = frmCpStats.WHEN_IN_FOCUSED_WINDOW;
     dialog.getRootPane().registerKeyboardAction(e -> window.dispose(), k, w);
    }     

    public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                StatsDisplay window = new StatsDisplay();
                window.frmCpStats.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    }
    public StatsDisplay() {
        initialize();
    }

Eclipse identifies errors Cannot be resolved or is not a field with VK_ESCAPE and WHEN_IN_FOCUSED_WINDOW, and dialog cannot be resolved with dialog. I'm quite unfamiliar with how swing actually works, so I haven't the foggiest idea why these errors are occurring.

R.McGuigan
  • 13
  • 5
  • Please post a valid [mcve] with your question, code we can compile, run and test. Question: are you importing the correct KeyEvent class, specifically `java.awt.event.KeyEvent`? – Hovercraft Full Of Eels Sep 04 '17 at 12:51
  • Also, `JFrame` does not inherit from `JComponent`, and so it does not have `JComponent` constants such as `WHEN_IN_FOCUSED_WINDOW`. But you should be checking the API for stuff like this before coming here, no? – Hovercraft Full Of Eels Sep 04 '17 at 12:52
  • I was using `org.eclipse.swt.events.KeyEvent`, using awt seems to have cleared up whatever was wrong with the `VK_ESCAPE` bit. I'm not hugely knowledgeable on Java, I've had to work on this a little blindly. – R.McGuigan Sep 04 '17 at 12:58
  • The API is your friend -- you will want to use it to exhaustion. – Hovercraft Full Of Eels Sep 04 '17 at 13:16
  • Again, please post a valid [mcve], a complete small program. You're still only posting snippets. Please be sure to read the link. – Hovercraft Full Of Eels Sep 04 '17 at 13:18
  • I think what's up there should be a simplified program, not sure what else it needs – R.McGuigan Sep 04 '17 at 13:28
  • You're missing imports, class structure, ... we should be able to copy, paste it into our ide's and run without modification. Please note my code posted below to see what I mean. Remember that you're asking *volunteers* to help you, and we request, no expect, that you make it as easy as possible to help. That's certainly not asking too much. – Hovercraft Full Of Eels Sep 04 '17 at 13:31
  • Why `window.dispose()`? What is "window"? – Hovercraft Full Of Eels Sep 04 '17 at 13:38
  • I'm not sure, the solution was from [this](https://stackoverflow.com/questions/38802417/how-to-escape-from-full-screen-mode-in-java) thread. I think it closes the window instead of just exiting fullscreen. Sorry about the confusion. – R.McGuigan Sep 04 '17 at 13:42
  • 1
    Don't blindly copy code. This is not how you learn to code and not how you ask questions on this site. – Hovercraft Full Of Eels Sep 04 '17 at 13:44

2 Answers2

0

You're almost there. Key Bindings works in this situation. Please note my MCVE:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class StatsDisplay {
    private JFrame frmCpStats;

    private void initialize() {
        frmCpStats = new JFrame();

        JPanel contentPane = (JPanel) frmCpStats.getContentPane();           
        int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
        InputMap inputMap = contentPane.getInputMap(condition);
        ActionMap actionMap = contentPane.getActionMap();

        KeyStroke escapeStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);

        inputMap.put(escapeStroke, escapeStroke.toString());
        actionMap.put(escapeStroke.toString(), new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                frmCpStats.dispose();
            }
        });

        frmCpStats.setTitle("CP Stats");
        Toolkit tk = Toolkit.getDefaultToolkit();
        int x = ((int) tk.getScreenSize().getWidth());
        int y = ((int) tk.getScreenSize().getHeight());
        frmCpStats.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frmCpStats.setSize(x, y);
        frmCpStats.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmCpStats.setUndecorated(true);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StatsDisplay window = new StatsDisplay();
                    window.frmCpStats.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public StatsDisplay() {
        initialize();
    }
}

Curious as to why you're calling window.dispose() when its the frmCpStats JFrame that you're actually trying to dispose of??

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks, this is almost what I need, however when I press escape the window closes as opposed to simply exiting fullscreen. – R.McGuigan Sep 04 '17 at 13:41
  • @R.McGuigan: but your code is calling `dispose()` within its action??? – Hovercraft Full Of Eels Sep 04 '17 at 13:42
  • @R.McGuigan: then change that code so it changes the window's state. You're using variables that have never been declared, making calls that you don't want to do .... I think I see what's going on -- you're trying to copy found code and use it without thinking what it does. Please don't do that, and certainly never ask a question with this type of code. Instead copy ideas but write your own code that makes sense. – Hovercraft Full Of Eels Sep 04 '17 at 13:43
  • Sorry, my knowledge of how Java functions is quite rudimentary, apologies for wasting your time with my unprofessional responses/question – R.McGuigan Sep 04 '17 at 13:59
-1

Remove these code lines

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