Question

In my main class I initially start out by creating the java frame. Later I create a new object from another class entitled 'keybinding' and pass it the JFrame. It then applies the action maps for certain keys to the root pane of the JFrame.

I was previously writing this application in the educational IDE BlueJ where this code worked fine. But I'm in the process of moving it to regular IDE, in this case JDeveloper. All the code works apart from the this keybinding.

With the below code, using JDev, calling getRootPane() returns null.

Stage.java

jf = new JFrame("The Title");
jf.setSize(800,600);
//etc
Keybinding keys = new Keybinding(this);

Keybinding.java

KeyStroke pressLeft = KeyStroke.getKeyStroke("LEFT");
stage.jf.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                          .put(pressLeft, "pressLeft");
Was it helpful?

Solution

The parameter to the KeyBinding class should just be

new KeyBinding(jf);

Then the code in the KeyBinding class can access the frame as follows:

public KeyBinding(JFrame frame)
{
    JRootPane rootPane = frame.getRootPane();
    rootPane.getInputMap(...)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top