FullScreen Swing Components Fail to Receive Keyboard Input on Java 7 on Mac OS X Mountain Lion

后端 未结 3 1710
野趣味
野趣味 2020-12-03 05:17

Update 12/21:

7u10 was recently released. Confirmed that:

  1. The issue still persists
  2. Thankfully, the workaround still function
相关标签:
3条回答
  • 2020-12-03 05:34

    Instead, use key bindings, as shown in this FullScreenTest. Also, consider a DocumentListener, shown here, for text components.

    0 讨论(0)
  • 2020-12-03 05:47

    This is because the component to which you added the other has now lost focus, you can fix this by either:

    • calling requestFocus() on the component instance to which you add KeyBindings

    or

    • alternatively use JComponent.WHEN_IN_FOCUSED_WINDOW with KeyBindings:

      component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0),
                              "doSomething");
      component.getActionMap().put("doSomething",
                               anAction);
      

    Reference:

    • How to Use Key Bindings
    0 讨论(0)
  • 2020-12-03 05:47

    I think I finally found a solution, registering click listeners against to the JFrame itself. (This is a class which extends JFrame, hence all the "this" references.)

    /**
     * Toggles full screen mode. Requires a lot of references to the JFrame.
     */
    public void setFullScreen(boolean fullScreen){
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice dev = env.getDefaultScreenDevice();//Gets the main screen
        if(!fullScreen){//Checks if a full screen application isn't open
            this.dispose();//Restarts the JFrame
            this.setVisible(false);
            this.setResizable(true);//Re-enables resize-ability.
            this.setUndecorated(false);//Adds title bar back
            this.setVisible(true);//Shows restarted JFrame
            this.removeMouseListener(macWorkAround);
            this.pack();
            this.setExtendedState(this.getExtendedState()|JFrame.MAXIMIZED_BOTH);//Returns to maximized state
            this.fullScreen = false;
        }
        else{
            this.dispose();//Restarts the JFrame
            this.setResizable(false);//Disables resizing else causes bugs
            this.setUndecorated(true);//removes title bar
            this.setVisible(true);//Makes it visible again
            this.revalidate();
            this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
            try{
                dev.setFullScreenWindow(this);//Makes it full screen
                if(System.getProperty("os.name").indexOf("Mac OS X") >= 0){
                    this.setVisible(false);
                    this.setVisible(true);
                    this.addMouseListener(macWorkAround);
                }
                this.repaint();
                this.revalidate();
            }
            catch(Exception e){
                dev.setFullScreenWindow(null);//Fall back behavior
            }
            this.requestFocus();
            this.fullScreen = true;
        }
    }
    
    private MouseAdapter macWorkAround = new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            MainGUI.this.setVisible(false);
            MainGUI.this.setVisible(true);
        }
    };
    
    0 讨论(0)
提交回复
热议问题