I\'m trying for more than 2 days to implement a specific requirement for a text editor window... unfortunately without success so far :(
The goal is to get a text editor
Below is the code to extract text from current line. You can use same logic to get required indexes and highlight text
private String getCurrentEditLine() {
int readBackChars = 100;
int caretPosition = scriptEditor.getCaretPosition();
if (caretPosition == 0) {
return null;
}
StyledDocument doc = scriptEditor.getStyledDocument();
int offset = caretPosition <= readBackChars ? 0 : caretPosition
- readBackChars;
String text = null;
try {
text = doc.getText(offset, caretPosition);
} catch (BadLocationException e) {
}
if (text != null) {
int idx = text.lastIndexOf("\n");
if(idx != -1) {
return text.substring(idx);
}else {
return text;
}
}
return null;
}