Search code examples
javaswingnetbeansjtextpane

Change font style of highlighted text java


Below is the code I have to set all text within my text pane to bold but how can I get only the text that I have highlighted to be bold and achieve the following?

private void jButtonBoldActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
    jTextPaneBody.setFont(jTextPaneBody.getFont().deriveFont(Font.BOLD));
}
  • If the highlighted text is normal, change it to bold when I click the button

  • If the highlighted text is in bold, change it to normal when I click the same button


Solution

  • Can't be done with a (J)TextField. If you need this functionality, you can use a JTextPane with a StyledEditorKit and a StyledDocument