We would like to know how to unHighlight the text in JTextPane.
import java.awt.Color; //from w ww .j ava 2s . c om import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JTextPane; import javax.swing.text.DefaultHighlighter; import javax.swing.text.Document; import javax.swing.text.Highlighter; import javax.swing.text.JTextComponent; public class Main extends JFrame { public static void main(String args[]) { new Main().setVisible(true); } Highlighter.HighlightPainter myHighLightPainter=new MyHighightPainter(Color.LIGHT_GRAY); private JScrollPane scrollPane; private JTextField searchText; private JTextPane textPane; private JButton search; public Main() { search = new javax.swing.JButton(); searchText = new javax.swing.JTextField(); scrollPane = new javax.swing.JScrollPane(); textPane = new javax.swing.JTextPane(); searchText.setText("test"); textPane.setText("this is a testtest "); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); search.setText("Search"); search.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { searchActionPerformed(evt); } }); scrollPane.setViewportView(textPane); getContentPane().add(scrollPane,"Center"); getContentPane().add(searchText,"North"); getContentPane().add(search,"South"); pack(); } public void highLight(JTextComponent component,String patteren){ try { Document doc=component.getDocument(); String text=component.getText(0,doc.getLength()); int pos=component.getCaretPosition(); if (pos==doc.getLength()) { pos=0; } int index=text.toUpperCase().indexOf(patteren.toUpperCase(),pos); if (index>=0) { component.setSelectionStart(index); component.setSelectionEnd(index+patteren.length()); component.getCaret().setSelectionVisible(true); } } catch(Exception e){ e.printStackTrace(); } } private void searchActionPerformed(java.awt.event.ActionEvent evt) { highLight(textPane, searchText.getText()); } } class MyHighightPainter extends DefaultHighlighter.DefaultHighlightPainter{ MyHighightPainter(Color color){ super(color); } }