0

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.

Steve Cohen
  • 4,679
  • 9
  • 51
  • 89
  • 1
    Post an [SSCCE](http://sscce.org) for better help sooner. – Guillaume Polet Sep 19 '12 at 21:20
  • Perhaps, but in general, in normal Swing as opposed to my horribly bastardized version of it, which component handles the arrow keys? Is it the Scroll Bar, the Scroll Pane, or the Text Pane? – Steve Cohen Sep 19 '12 at 21:24
  • 1
    See also [*Key Bindings*](http://stackoverflow.com/a/8639211/230513), which lists the defaults for each L&F. – trashgod Sep 19 '12 at 21:36
  • time to sleep, great comment about Keys and differencies for concrete L&F – mKorbel Sep 19 '12 at 21:38
  • @trashgod - yeah, key bindings is always my first stop in such cases and in fact I've already been there. How else do you think I knew the names of the input map and actions? But in this case, that didn't answer my question. – Steve Cohen Sep 19 '12 at 21:44
  • Actually, KeyBindings WAS the answer, I just looked in the wrong place. The EditorPane (from which Text Pane is descended) had all that good stuff. – Steve Cohen Sep 19 '12 at 21:51
  • @SteveCohen: Sound like an interesting odyssey. You can [answer your own question](http://meta.stackexchange.com/q/17463/163188). Possible related [example](http://stackoverflow.com/a/7203419/230513) of recycling actions. – trashgod Sep 19 '12 at 21:57
  • Well, a partial solution anyway. I disabled the actions on the text pane for the four keystrokes. Then I mapped them in my scroll pane class. Shift=Up and Shift-Down now work. Shift-Left and Shift-Right do not. – Steve Cohen Sep 19 '12 at 22:18

3 Answers3

2
  • reference version for Steve :-)

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

  • DU love this forum too,

  • SSCCE could opening any doors for volunteers on 2nd side. isn't it

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Part of the problem is, though, what do these named actions actually DO? In the scroll pane page, we have CTRL_PAGE_UP mapped to "scrollLeft" and CTRL_PAGE_DONN mapped to "scrollRight". That's a bit curious. Does it mean go to the top or bottom of the Document? There are also the "scrollHome" and "scrollEnd" actions. I've tried mapping both sets to my Shift-Right and Shift-Left keys (after disabling them in the Text Pane) and neither does anything. But the same mechanism works for Shift-Up and Shift-Down. Very curious. – Steve Cohen Sep 19 '12 at 22:53
  • Well, delving into the Swing source code, it seems pretty clear that I want "scrollHome" and "scrollEnd", not "scrollLeft" and "scrollRight". But, in any case, they don't work. – Steve Cohen Sep 19 '12 at 23:09
  • @Steve Cohen see answer by StanislavL – mKorbel Sep 20 '12 at 09:55
1

As mKorbel correctly shows there is an action "page-up" and "page-down" (see 3rd screen). So just use ActionMap and replace the action with yours.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • page-up and page-down are not what I want. I want scrolling actions that leave the caret alone, not caret-moving actions, which is what I think page-up and page-down are, but even if these actions don't move the caret, I've already got what I need from ScrollPane scrollUp and scrollDown actions for those. What I can't get to work are the "scrollHome" and "scrollEnd" actions. Thanks – Steve Cohen Sep 20 '12 at 14:04
1

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

...
        disableAction("caret-down"); // down arrow
        disableAction("caret-up");   // up arrow
        disableAction("selection-backward"); // shift-left-arrow
        disableAction("selection-forward");  // shift-right-arrow
        disableAction("selection-up");  //shift-up-arrow
        disableAction("selection-down"); // shift-down-arrow

    }

    private void disableAction(String name) {
        Action a = getActionMap().get(name);
        if (a != null) {
            a.setEnabled(false);
        }
    }

In the ScrollPane

import static javax.swing.KeyStroke.getKeyStroke;
import static java.awt.event.KeyEvent.*;
...
    private void remapShiftedArrowKeys() {
        InputMap map = (InputMap)UIManager.get("ScrollPane.ancestorInputMap");
        map.put(getKeyStroke(VK_DOWN, SHIFT_DOWN_MASK), "scrollDown");
        map.put(getKeyStroke(VK_UP, SHIFT_DOWN_MASK), "scrollUp");
        map.put(getKeyStroke(VK_LEFT, SHIFT_DOWN_MASK), "scrollHome");
        map.put(getKeyStroke(VK_RIGHT, SHIFT_DOWN_MASK), "scrollEnd");
    }

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,

Steve Cohen
  • 4,679
  • 9
  • 51
  • 89