How to prevent JScrollPane arrow key handling from moving caret when Scroll Pane wraps Text Pane

前端 未结 2 1985
情书的邮戳
情书的邮戳 2021-01-17 05:23

I have the following requirements:

I need a scrollable JTextPane. The user may type into this text pane, or text may be inserted into it that is not typed by the us

相关标签:
2条回答
  • 2021-01-17 05:35

    You're going to have to modify the KeyBindings

    Try this to start with

    InputMap im = textArea.getInputMap(WHEN_FOCUSED);
    ActionMap am = textArea.getActionMap();
    
    am.get("caret-down").setEnabled(false);
    am.get("caret-up").setEnabled(false);
    

    Now that you have that working, you need to worry about all these

    selection-up = shift pressed UP
    caret-next-word = ctrl pressed RIGHT
    selection-previous-word = shift ctrl pressed LEFT
    selection-up = shift pressed KP_UP
    caret-down = pressed DOWN
    caret-previous-word = ctrl pressed LEFT
    caret-end-line = pressed END
    selection-page-up = shift pressed PAGE_UP
    caret-up = pressed KP_UP
    delete-next = pressed DELETE
    caret-begin = ctrl pressed HOME
    selection-backward = shift pressed LEFT
    caret-end = ctrl pressed END
    delete-previous = pressed BACK_SPACE
    selection-next-word = shift ctrl pressed RIGHT
    caret-backward = pressed LEFT
    caret-backward = pressed KP_LEFT
    selection-forward = shift pressed KP_RIGHT
    delete-previous = ctrl pressed H
    unselect = ctrl pressed BACK_SLASH
    insert-break = pressed ENTER
    selection-begin-line = shift pressed HOME
    caret-forward = pressed RIGHT
    selection-page-left = shift ctrl pressed PAGE_UP
    selection-down = shift pressed DOWN
    page-down = pressed PAGE_DOWN
    delete-previous-word = ctrl pressed BACK_SPACE
    delete-next-word = ctrl pressed DELETE
    selection-backward = shift pressed KP_LEFT
    selection-page-right = shift ctrl pressed PAGE_DOWN
    caret-next-word = ctrl pressed KP_RIGHT
    selection-end-line = shift pressed END
    caret-previous-word = ctrl pressed KP_LEFT
    caret-begin-line = pressed HOME
    caret-down = pressed KP_DOWN
    selection-forward = shift pressed RIGHT
    selection-end = shift ctrl pressed END
    selection-previous-word = shift ctrl pressed KP_LEFT
    selection-down = shift pressed KP_DOWN
    insert-tab = pressed TAB
    caret-up = pressed UP
    selection-begin = shift ctrl pressed HOME
    selection-page-down = shift pressed PAGE_DOWN
    delete-previous = shift pressed BACK_SPACE
    caret-forward = pressed KP_RIGHT
    selection-next-word = shift ctrl pressed KP_RIGHT
    page-up = pressed PAGE_UP
    
    0 讨论(0)
  • 2021-01-17 05:40

    What if you let user place caret e.g. to let him select and copy some text?

    I would add a DocumentFilter (or override insertString() method of the Document) and in all cases perform insert in the doc.getLength() position and resetting the caret to the doc.getLength() position after the insert.

    0 讨论(0)
提交回复
热议问题