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.
Steve cohen
source share