which component handles arrow KeyEvents when JTextPane is embedded in JScrollPane?

北城以北 提交于 2019-12-23 04:47:16

问题


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.


回答1:


  • reference version for Steve :-)

  • DU love this forum too,

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




回答2:


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.




回答3:


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,



来源:https://stackoverflow.com/questions/12502954/which-component-handles-arrow-keyevents-when-jtextpane-is-embedded-in-jscrollpan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!