title: How to change the text of selected text in Java?
body: <p>I want to change the text of selected text in JTextArea.</p>
  
  <p>For example when i press button i want the selected text to be change (Original Text Selected - I want to replace like this when i press the button : Replace:Original Text Selected) this is what i am trying to do in my code,</p>
  
  <pre><code>String replacement = "Replace:" + messageBodyText.getSelectedText() ";
  </code></pre>
  
  <p>but i have no idea how to change only selected text i am trying to do something but i am changing entire text of JTextArea Hope you understood my question ?</p>
  
  <p>Thanks to Hovercraft Full Of Eels he solved my problem this is my code for other people who are facing the same problem :   </p>
  
  <pre><code>int start = messageBodyText.getSelectionStart();
              int end = messageBodyText.getSelectionEnd();
  
              StringBuilder strBuilder = new StringBuilder(messageBodyText.getText());
              strBuilder.replace(start, end, "Replace:" + messageBodyText.getSelectedText() + ".");
              messageBodyText.setText(strBuilder.toString());
  </code></pre>