Prioritising keyboard focus

S

Simon Andrews

I have a swing application which contains several panels. I want one of
the panels to respond to the arrow keys and have registered accelerators
in a JMenu which do this correctly.

However, one of the other panels contains a JTree, and when this is
selected with the mouse it grabs the arrow key events first and they
don't make it back to the menu.

I've tried removing the binding in the tree by doing

tree.getInputMap().put(arrowKeyStroke,"none")

...for each arrow key. This stops the tree from responding to the key,
but it doesn't allow the event to pass back to the menu.

I've also tried using:

tree.unregisterKeyboardAction(arrowKeyStroke)
and
tree.getInputMap.remove(arrowKeyStroke)

...which didn't seem to have any effect at all.

How can I stop the JTree from capturing these events at all?

Cheers

Simon.
 
S

Simon Andrews

Simon said:
I have a swing application which contains several panels. I want one of
the panels to respond to the arrow keys and have registered accelerators
in a JMenu which do this correctly.

However, one of the other panels contains a JTree, and when this is
selected with the mouse it grabs the arrow key events first and they
don't make it back to the menu.

Replying to myself in case anyone else can benefit from this.

I've got around this in the end by making a subclass of JTree which
immediately gives away keyboard focus as soon as it gets it.

private class UnfocusableTree extends JTree implements FocusListener {

public UnfocusableTree (TreeModel m) {
super(m);
addFocusListener(this);
}

public void focusGained(FocusEvent e) {
application.requestFocus();
}

public void focusLost(FocusEvent e) {}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top