I am working with a terminal emulator, whose main view placeholder is a JTextPane
. Whenever a character is typed, out.write(c)
is executed, and then a function is called that does the styling of characters and inserts the text strings in JTextPane doc. Every time the key is pressed, content is redrawn.
The emulator doesnt perform functions related to arrow keys(up and down for command history),but uses Ctrl-F, Ctrl-B, Ctrl-P, Ctrl-N
combinations. What is happening, is that the caret keeps blinking at the upper most top left corner of JTextPane. It doesn't moves according to the terminal-cursor-focus. Surprising thing is, when I type something in the emulator, use Ctrl-B
to go back to previously entered characters, and press backspace, the correct character is removed, which means that the cursor focus is correctly working as it behaves in normal command prompt. Its just that the caret is not getting that position somehow.
I tried this approach:
int len = terminalArea.getDocument().getLength();
terminalArea.setCaretPosition(len);
but this always points the caret to the last entered character, and doesn't moves back and forth when Ctrl-B
and Ctrl-F
are pressed. I need help in this regard, i.e. how do I get the position within the emulator to which the shell cursor is pointing to, and show caret at that point?