Search code examples
javaswinguser-interfacejtextpanestrikethrough

I need help updating my JTextPane by adding a strikethrough


I am working on a user-interface and I have a JTextPane in a JPanel and in the JTextPane I have added strings using JTextPane.getStyledDocument().insertString(). Now I want to update the JTextPane and put a strike through through those words.

How do I go about updating the words after a JButton is clicked? I don't want all the strings in the JTextPane to get a strike-through, just certain ones.


Solution

  • In actionListener of jButton try this.

    jTextPane.setContentType( "text/html" );
    jTextPane.setText("<strike> Words to strike </strike> Words not to Strike");
    

    Use your logic to strike the required words.

    You can use jTextPane.getSelectedText() or jTextPane.getText();