0

Using a JEditorPane, is there any way to tell if the caret is currently on the last line? I cannot find anything in the API or more importantly JTextComponent of which JEditorPane is derived. I need to find this out when the user uses the down arrow key to move down the text. My current idea is this:

private boolean isEndOfText() {
  int tmpCurrent = editor.getCaretPosition();

  editor.getActionMap().get(DefaultEditorKit.endLineAction).actionPerformed(null);
  int tmpEnd = editor.getCaretPosition();
  try { editor.setCaretPosition(tmpEnd + 1); } catch (Exception e) { editor.setCaretPosition(tmpCurrent); return true; }
  editor.setCaretPosition(tmpCurrent);
  return false;
}

This code would run whenever the down key is pressed and would return whether or not it is in fact the end of the text by detecting if an error occurs if the caret is being put after the last possible position which would be the end of the line (if it is in fact the last line) otherwise it means the end of text has not been reached.

kleopatra
  • 51,061
  • 28
  • 99
  • 211

2 Answers2

1

You should be able to use the Text Utilities. One method returns the total lines and another method return the line at the caret.

I've never played with a JEditorPane that much since I don't like its HTML support. You might also be able to use editor.getDocument().getLength() to determine if the caret is at the end of the document. This will work with a JTextArea or a JTextPane that only displays text, not HTML. Not sure how it works in a JEditorPane.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Well the pane is still only using straight text but with syntax highlighting so I don't believe HTML would case me problems regardless. –  Nov 09 '10 at 11:33
  • My point about HTML is that the Document is full of HTML tags. So if you do editor.getText(), versus editor.getDocument().getText() I believe you get two different string, one will only contain the text and the other will contain all the tags as well. So you need to be careful how you compare the offsets. I prefer using a JTextPane with attribute to stylize the text then the text strings are the same since the attributes are not part of the document. Anyway did my suggestion help solve the problem? – camickr Nov 09 '10 at 16:15
  • It is iffy at best. While I do like seeing how other people tackle the same problem, I already have a current line detection system in place by checking use of the arrow keys as well as delete, enter, and backspace. I also have a system of keeping the amount of lines that is arguably more efficient than the methods given. I think my answer here will actually come from myself doing more coding which will in turn add more functionality that I can later use to make the existing functions more efficient. Is there a term for that? Anyway thanks for the answer. –  Nov 09 '10 at 23:22
  • I'm impressed that your code to get the number of lines is more efficient than the 2 lines of code I provided. Maybe you can share your solution? – camickr Nov 10 '10 at 05:41
0

There's probably a better way, but you could try this:

return editor.getText().indexOf("\n", editor.getCaretPosition()) == -1;
lins314159
  • 2,510
  • 1
  • 16
  • 19