0

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?

user123
  • 15
  • 6
  • could be a nice question based on SSCCE/MCVE short, runnable, compilable with hardcoded value for JTextPane in local variable – mKorbel May 23 '14 at 11:04
  • The program uses third party library, I can't produce SSCCE actually, as it is a long code.. – user123 May 23 '14 at 11:07
  • 1
    S in the SSCCE means **short** so just create an example to illustrate the problem. Of course nobody wants to investigate the whole project you have. – StanislavL May 23 '14 at 11:11
  • @mKorbel: I have several examples with hard coded values of JTextPane, but caret moves there perfectly :) My case is, characters are appended in a string builder, which is then styled and put as string for JTextPane document – user123 May 23 '14 at 11:20
  • Then you have to figure out what's the difference between the working examples and your actual code. – StanislavL May 23 '14 at 11:59
  • Those examples do nothing with the caret, there is no `setCaretPosition()` etc. – user123 May 23 '14 at 12:02
  • `Those examples do nothing with the caret, there is no setCaretPosition() etc.` - then the problem is how you use `setCaretPosition()`. Post your SSCCE that demonstrates how you are attempting to do this. That is the point of a SSCCE. Isolate what is different and post the SSCCE so we can look at it to see if we notice a problem. – camickr May 23 '14 at 15:12

0 Answers0