0

On my Mac, fullscreen JFrames initially have key bindings that do not work, and the computer outputs alert beeps each time I try to type. There is a workaround, though, after fully initializing my JFrame, I added these lines of code and all the errors stopped:

   setVisible(false);
   setVisible(true);

Here's the source of this workaround: http://mail.openjdk.java.net/pipermail/macosx-port-dev/2012-November/005109.html

Another problem which is yet to be solved is adding a mouse adapter to my full screen JFrame application. Whenever I clicked, the focus changed--to where, I couldn't quite tell, but setting the inputmap of my keybindings to each one of the three options didn't help.

I even tried redoing the workaround when the mouse was clicked by adding this: event.getComponent().setVisible(false); event.getComponent().setVisible(true); but to no avail.

Here is an SSCCE of the problem (it will only show up on a mac):

import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class FocusTest extends JFrame{
    private static final int PREF_W = 400;
    private static final int PREF_H = PREF_W;

    public FocusTest() {
       MyPanelDescendent myPanelDescendent = new MyPanelDescendent();
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       getContentPane().add(myPanelDescendent);
       pack();
       setLocationByPlatform(true);
       setVisible(true);

       KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
       Action escapeAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
             dispose();
             System.exit(0);
            }
       };
       getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(escapeKeyStroke, "ESCAPE");
       getRootPane().getActionMap().put("ESCAPE", escapeAction);

       GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
       GraphicsDevice gs = ge.getDefaultScreenDevice();
       gs.setFullScreenWindow(this);

       setVisible(false);
       setVisible(true);
    }
    private class MyPanelAscendent extends JPanel{
        public MyPanelAscendent() {
           setFocusable(true);
           requestFocusInWindow();

             getInputMap(0).put(KeyStroke.getKeyStroke("pressed A"), "pressed");
             getActionMap().put("pressed", new AbstractAction() {
                   @Override public void actionPerformed(ActionEvent e) {
                       if (e.getActionCommand().equalsIgnoreCase("a")) {
                           System.out.println("a was pressed");
                       }
                   }
             });

           addMouseListener(new MyAdapter());
        }
    }
    private class MyPanelDescendent extends MyPanelAscendent {
        public MyPanelDescendent() {
            super();
        }
    }

    private class MyAdapter extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent event) {
            event.getComponent().setVisible(false);
            event.getComponent().setVisible(true);
            System.out.println("clicked");
        }
    }

    @Override
    public Dimension getPreferredSize() {
       return new Dimension(PREF_W, PREF_H);
    }



    public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
          public void run() {
             new FocusTest();
          }
       });
    }
}

If you press the a key, then click, then do it again, it will not work. The same goes for the escape key: if you click then try to use it, it won't work.

here is an example for fullscreen posted by trashgod which I've also found impossible to make work with both keybindings, fullscreen, and a mouse adapter at the same time.

Community
  • 1
  • 1
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
  • `the computer outputs alert beeps each time I try to type` I don't use a Mac but I noticed the requestFocusInWindow() method in your constructor. This does nothing because you can only request focus on a component that is displayed on a visible GUI. So maybe try invoking that method after you set the frame to full screen (instead of the setInvisible/Visible). Sounds to me likes its beeping because no component has focus. – camickr Nov 04 '13 at 06:42
  • Yeah, I just edited my question because it wasn't clear enough that I had already solved that with the work around. This is a recreation of a small part in a larger application I have definitely has the panel on a visible GUI. Now, after I click with the mouse, there is no beeping when I type, which tells me something must have focus? However, there is no response from the key bindings. – michaelsnowden Nov 04 '13 at 07:26
  • Do you have any ideas? I'm totally lost on this still, and it's driving me crazy. – michaelsnowden Nov 06 '13 at 00:57
  • Have you tried adding the mouse adapter directly to the JFrame instead of the JPanel? – Skylion Dec 27 '13 at 18:36
  • Sorry, I haven't edited this application for a while, but I'll reply here with an answer to that if I ever try it. – michaelsnowden Jan 02 '14 at 10:50

0 Answers0