0

I have a simple frame that have a button to toggle whether that frame is full screen or not. I want to have it so that the user only has to press the enter key to toggle the full screen. I am very new to Java. I would like the event listener to be in private FrontEnd()

    import java.awt.event.*;
    import javax.swing.*;

    public class FrontEnd extends JFrame {
        private boolean FullScreenMode = false;
        private int PrevX, PrevY, PrevWidth, PrevHeight;

        public static void main(String[] args) {
            FrontEnd frame = new FrontEnd();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(480, 320);
            frame.setVisible(true);
        }

        private FrontEnd() {

            JPanel contentPane = new JPanel();
            setContentPane(contentPane);

            // From Here starts the trick
            FullScreenEffect effect = new FullScreenEffect();

            JButton fullscreenButton = new JButton("Fullscreen Mode");
            fullscreenButton.addActionListener(effect);

            contentPane.add(fullscreenButton);
            fullscreenButton.setVisible(true);
        }

        private class FullScreenEffect implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (!FullScreenMode) {
                    PrevX = getX();
                    PrevY = getY();
                    PrevWidth = getWidth();
                    PrevHeight = getHeight();

                    dispose();
                    setUndecorated(true);

                    setBounds(0, 0, getToolkit().getScreenSize().width, getToolkit().getScreenSize().height);

                    setVisible(true);
                    FullScreenMode = true;
                } else {
                    dispose();
                    setUndecorated(false);

                    setBounds(PrevX, PrevY, PrevWidth, PrevHeight);

                    setVisible(true);
                    FullScreenMode = false;
                }
            }
        }
leocborges
  • 4,799
  • 5
  • 32
  • 38
Ross
  • 2,463
  • 5
  • 35
  • 91
  • What's the problem? Are you getting an error? – Steve Smith Mar 08 '17 at 11:57
  • nothing has a error. The button works as expect. but instead of a button click, i want a keypress. so there no button and the user presses enter to toggle fullscreen – Ross Mar 08 '17 at 11:59
  • Well, [Using Keylistener](http://stackoverflow.com/questions/21997130/how-to-use-keylistener-with-jframe) or better a [key binding](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html). You should first try those solution. – AxelH Mar 08 '17 at 12:12
  • As similar issue is examined [here](http://stackoverflow.com/q/7456227/230513). – trashgod Mar 08 '17 at 20:05

2 Answers2

2

I have a simple frame that have a button to toggle whether that frame is full screen or not. I want to have it so that the user only has to press the enter key to toggle the full screen.

Don't use a KeyListener!

Swing was designed to use Key Bindings to map a KeyStroke to an Action.

The easiest way to do this is to make the button the default button for the frame, then the Enter key will activate the button.

frame.getRootPane.setDefaultButon( fullScreenButton );
camickr
  • 321,443
  • 19
  • 166
  • 288
-2

Manage to get it to work. The working code is below:

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class FrontEnd extends JFrame implements KeyListener {
    private boolean FullScreenMode = false;
    private int PrevX, PrevY, PrevWidth, PrevHeight;

    public static void main(String[] args) {
        FrontEnd frame = new FrontEnd();
        frame.setTitle("Frame Title");
        frame.setResizable(false);
        frame.setSize(600, 600);
        frame.setMinimumSize(new Dimension(600, 600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.black);
        frame.pack();
        frame.setVisible(true);
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER)
            FullScreenToggle();
        else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
            ExitApplication();
    }

    public void keyReleased(KeyEvent e) {
    }
    public void keyTyped(KeyEvent e) {
    }

    public FrontEnd(){
        addKeyListener(this);
    }

    public void FullScreenToggle() {
        if (!FullScreenMode) {
            PrevX = getX();
            PrevY = getY();
            PrevWidth = getWidth();
            PrevHeight = getHeight();

            dispose();
            setUndecorated(true);

            setBounds(0, 0, getToolkit().getScreenSize().width, getToolkit().getScreenSize().height);

            setVisible(true);
            FullScreenMode = true;
        } else {
            dispose();
            setUndecorated(false);

            setBounds(PrevX, PrevY, PrevWidth, PrevHeight);

            setVisible(true);
            FullScreenMode = false;
        }
    }

    public void ExitApplication() {
        System.exit(0);
    }
}
Ross
  • 2,463
  • 5
  • 35
  • 91
  • posting working code without an explanation is not very useful. – Martin Serrano Mar 08 '17 at 13:21
  • like I said above, I am very new to Java and don't fully understand how it works myself. But posting the working code is better than not having a working solution at all. Therefor may help someone. – Ross Mar 08 '17 at 13:38
  • (1-) In addition to this being a non-swing solution (see my answer), variable names should not start with an upper case. Method names should not start with an upper case. Solutions you post should promote standard Java coding conventions. – camickr Mar 08 '17 at 16:34