How to prevent the JScrollPane from manipulating the carriage when the scroll bar wraps the text bar

I have the following requirements:

I need a scrollable JTextPane. The user can enter into this text box, or the text can be inserted into it, which is not entered by the user. Think of something like an IM window. Although the window should be scrollable to allow the user to view the text that was previously entered, the carriage should not move from its position at the end of the text. Any text entered by the user will always be displayed at the end.

In JTextPane, when the user scrolls the scroll bar, the carriage does not move. The viewport is configured. However, when the user presses the up and down arrow keys, the JTextPane carriage moves with it (regardless of whether the window scrolls or not).

I want the up arrow to work the same way as moving the scrollbar with the mouse. The arrow keys should not have anything to do with the carriage.

I tried the following approaches, without success: 1) add a No-op action to the Keymap for my text panel class (using JTextPane.addKeymap () and Keymap.addActionForKeyStroke ()). This stops the cursor moving, but prevents the action from being passed to the scroll pane to scroll the view. 2) remove the arrow keys from the keyboard layout for my text panel class. This affects all JTextPanes in my application, which is not what I want.

I want to add an action to my TextPane text layout, which simply calls the ScrollPane action for the up and down arrows.

What is the best way to do this?

An opportunity that arises for me is to implement a KeyListener (which gets the key stroke of the key before the layout) to catch these keys, and then scroll manually. But this, it would seem, requires me to calculate font sizes, etc. Is there an easier way?

It would be ideal if there was some way to “tie” the caret to what was at the end of the text.

0
source share
2 answers

You will need to change 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 are working, you need to worry about all of 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 
+3
source

What if you allow the user to place a carriage, for example? let him select and copy some text?

I would add a DocumentFilter (or override the document’s insertString () method) and in all cases perform the insert in doc.getLength () and reset the carriage to the doc.getLength () position after insertion.

+1
source

All Articles