I am trying to make a Text Editor using Java Swing. In that I am using JEditorPane instead of JTextArea. I am facing problems in deleting selected text and replacing selected text from the JEditorPane. The Code I am Using is:
public void delete(JEditorPane txt)
{
int start = txt.getSelectionStart();
int end = txt.getSelectionEnd();
String startText = txt.getText().substring(0,start);
String endText = txt.getText().substring(end,txt.getText().length());
txt.setText(startText + endText);
}
The problem I am facing here is that, when I consider the value from getSelectionStart() and getSelectionEnd(), They don't consider newline character, but while using substring, newline character is being considered. So if I use this code on a line before which there are 5 newline characters, then instead of deleting selected text, text gets deleted from a position which is 5 less then the selected text. Same is happening with Replace. Please Help.