1

SOLVED 11/03/13:

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

It turned out to be a problem with the fact that I was using a Mac, and I found an answer to it above. Hope this helps any other Mac users with the same problem.

I have created a game which requires focus in a JPanel, but ever since I made my JFrame fullscreen, the game panel, mainPanel, can't receive focus. Essentially, what I have looks like this SSCCE:

package com.StackOverflow;

import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JFrame
{
    private JPanel panel; //private MainPanel mainPanel;

    public MainClass()
    {
        panel = new JPanel(true);   // mainPanel = new MainPanel();

        // I already set these attributes in my parent class of MainPanel, MyPanel
        panel.setLayout(null);  
        panel.setOpaque(false);
        panel.setFocusable(true);
        panel.setVisible(true);

        setUndecorated(true);
        setLocation(0, 0);
        add(panel); // add(mainPanel)
        pack();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        gs.setFullScreenWindow(this);

        System.out.format("%-30s:%10s\n", "panel is displayable", panel.isDisplayable());
        System.out.format("%-30s:%10s\n", "panel is focusable", panel.isFocusable());
        System.out.format("%-30s:%10s\n", "panel is visible", panel.isVisible());
        System.out.format("%-30s:%10s\n", "panel can request focus", panel.requestFocusInWindow());

//          mainPanel.run();
    }
    public static void main(String args[])
    {
        new MainClass();
    }
}

With the output:

mainPanel is displayable      :      true
mainPanel is focusable        :      true
mainPanel is visible          :      true
mainPanel can request focus   :     false

From Eclipse's definition of Component.requestFocusInWindow():

Requests that this Component get the input focus, if this Component's top-level ancestor is already the focused Window. This component must be displayable, focusable, visible and all of its ancestors (with the exception of the top-level Window) must be visible for the request to be granted. Every effort will be made to honor the request; however, in some cases it may be impossible to do so. Developers must never assume that this Component is the focus owner until this Component receives a FOCUS_GAINED event.

Specifically and perhaps besides the point, I have made a MyPanel class which extends JPanel and implements AcionListener and Runnable. I extended that class with MainPanel, and added a MainPanel object to a fullscreen JFrame.

MainPanel is where I control and display a game which has a keylistener. The game worked totally fine before I made my JFrame fullscreen, but now the problem is it cannot receive focus, even though everything in it is still running (enemy still chases player across screen, but player can't respond.)

I added a focuslistener to the parent class, MyPanel, but it never lost or gained focus, until I used straight up mainPanel.requestFocus(), but the output told me that it gained focus then immediately lost it. I even tried setFocusable(false) on the JFrame.

When I used setVisible(true) on JFrame, then revalidate(), the output changed, and I was told the mainPanel.requestFocusInWindow() changed to true. The output then told me that mainPanel gained focus, lost focus, and then gained focus again. The whole time, however, my mac just responded by beeping every time I pressed a key. Sometimes, requestFocusInWindow() returned false, but it still said mainPanel gained, lost, then ultimately gained the focus.

In the second part of the documentation on requestFocusInWindow(), it says this:

This method returns a boolean value. If false is returned, the request is guaranteed to fail. If true is returned, the request will succeed unless it is vetoed, or an extraordinary event, such as disposal of the Component's peer, occurs before the request can be granted by the native windowing system. Again, while a return value of true indicates that the request is likely to succeed, developers must never assume that this Component is the focus owner until this Component receives a FOCUS_GAINED event.

That part is way over my head, so even if you can't solve this, if you know what that means, that might put me on the right track.

I will be posting updates on this if I find a solution.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.