OK here we go again. Steve has to program another non-standard set of key strokes.
We have an editable JTextPane embedded in a JScrollPane. This pane correctly handles the Up and Down arrow keys, but I can’t figure out how. If I could figure out how, I could implement the nonstandard things I need to implement.
Specifically, because the PageDown key is globally mapped to doing another function we don’t do the default actions for PageUp, PageDown,Ctrl-PageUp and Ctrl-PageDown. Instead we want to map these functions to the shifted arrow keys, not the ones on the numeric keypad.
Specifically in the JScrollPane class’s ancestor input map ((InputMap)UIManager.get(“ScrollPane.ancestorInputMap”);) we add the
- Shifted Down Arrow key to the Ancestor input map pointing to
the”scrollDown” action - Shifted Up Arrow key to the Ancestor input map pointing to the
“scrollUp” action - Shifted Left Arrow key to the Ancestor input map pointing to the
“scrollHome” action - Shifted Right Arrow key to the Ancestor input map pointing to the
“scrollEnd” action
None of these keystrokes do anything. I’vwe even overridden the processKeyEvent() and processKeyBinding() methods of JComponent to log what was going on, and I find that these methods are never fired by these keystrokes. Also, the plain standard up arrow and down arrow keystrokes do not fire these methods, even though these keystrokes do work.
So it seems clear that something else is handling these keystrokes. But what component is that?
And yes, the text pane does have focus when I am trying this.
OK, trashgod was basically right. The solution was to use the KeyBindings names for the action. The delay in finding the right answer was due to a stray bit of code that was undoing the mapping elsewhere.
More specifically, we disable the default keystrokes in the JTextPane and then add them to the input map of the Scroll Pane, mapped to their new actions.
In the TextPane constructor
In the ScrollPane
note that we don’t have to map shifted Up and Down keys because the JScrollPane already does what we want with those keys. It is simply enough to unmap them from the JTextPane. Whereas these other four keystrokes are completely non-standard and must also be remapped in the Scroll Pane.
Thanks for all your help!
Oh, and the actual answer is that the JTextPane normally handles those arrow keys of course. To do what I wanted I had to defeat that and map appropriately in the scroll Pane,