1

I'm trying to build a web browser in java , i want to have a full screen when the user presses fullScr button and exits from full screen mode when F11 key is pressed.

Here is the part of my code(it extends JFrame)

public Hello() {
    currentScene();
    setResizable(true);
    JPanel buttonPanel1 = new JPanel();
    backButton.setIcon(new ImageIcon(Hello.class.getResource("/rsz_left_custom.png")));
    buttonPanel1.add(backButton);
    forwardButton.setIcon(new ImageIcon(Hello.class.getResource("/1813406178.png"));
    buttonPanel1.add(forwardButton);
    buttonPanel1.add(locationTextField, BorderLayout.EAST);
    JPanel buttonPanel2 = new JPanel(new BorderLayout(5, 5));
    fullScr.setIcon(new ImageIcon(Hello.class.getResource("/rsz_gnome-view-fullscreensvg (1).png")));
    fullScr.setPreferredSize(new Dimension(40, 40));
    fullScr.setBorderPainted(false);
    fullScr.setBackground(Color.decode("#330300"));
    fullScr.setLocation(1050, 25);
    fullScr.setToolTipText("Go Huge");
    //enters full screen mode when button is clicked
    fullScr.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            buttonPanel.setVisible(false);
            try{
            setUndecorated(true);
            }
            catch(Exception e){
                System.out.println(e);
            }
        }
    });
    //exits full screen mode when F11 is pressed
    addKeyListener(new KeyAdapter() {
           public void keyPressed(KeyEvent ke) {  // handler
        if(ke.getKeyCode() == KeyEvent.VK_F11){
            buttonPanel.setVisible(true);
            setUndecorated(false);
        }
       }});
    fullScr.setEnabled(true);
    buttonPanel2.add(fullScr, BorderLayout.LINE_END);
    buttonPanel.setBackground(Color.decode("#330300"));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    buttonPanel.add(buttonPanel1, BorderLayout.WEST);
    buttonPanel.add(buttonPanel2, BorderLayout.EAST);
    JPanel lblBar = new JPanel(new BorderLayout());
    lblBar.add(lblStatus, BorderLayout.CENTER);
    fPane.add(jfxPanel);
    fPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    totalPane.add(buttonPanel, BorderLayout.NORTH);
    totalPane.add(fPane, BorderLayout.CENTER);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    getContentPane().add(totalPane);
    setPreferredSize(new Dimension(1024, 600));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
}

But when i compile it it gives me error like this:

java.awt.IllegalComponentStateException: The frame is displayable.
BUILD SUCCESSFUL (total time: 30 seconds)

And it doesn't exit full screen mode when F11 is pressed .What should i do? Thanks in advance.

IAmBlake
  • 457
  • 6
  • 21
  • 1
    [This](http://stackoverflow.com/questions/875132/how-to-call-setundecorated-after-a-frame-is-made-visible) is what you're looking for. – Mordechai Dec 11 '16 at 19:31
  • thanks that's exactly what i'm looking for, but can you please suggest me why my keylistener isn't working? – IAmBlake Dec 11 '16 at 19:36

2 Answers2

1

The exception is explained here: Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable

A workaround is described here: Dynamically show and hide JFrame decorations

Community
  • 1
  • 1
Zander Brown
  • 637
  • 8
  • 16
0

You have to add the key listener to the component it should listen to

theComponent.addKeyListener(new KeyAdapter() { ....
  • I guess from the code the OP provided, that he/she extends the `JFrame` class; so your answer is helpless. – Mordechai Dec 11 '16 at 19:44