List of usage examples for javax.swing JTextPane setHighlighter
@BeanProperty(expert = true, description = "object responsible for background highlights") public void setHighlighter(Highlighter h)
From source file:Main.java
public static void main(String[] args) { JFrame f = new JFrame(); JPanel panel = new JPanel(); JTextPane textPane = new JTextPane(); JTextField tf = new JTextField("is"); String word = ""; Highlighter highlighter = new UnderlineHighlighter(null); textPane.setHighlighter(highlighter); textPane.setText("This is a test"); panel.setLayout(new BorderLayout()); panel.add(new JLabel("Enter word, then press ENTER key: "), "West"); panel.add(tf, "Center"); final WordSearcher searcher = new WordSearcher(textPane); tf.addActionListener(e -> {/*from w w w .ja v a2s . c o m*/ String w = tf.getText().trim(); int offset = searcher.search(w); if (offset == -1) { return; } try { textPane.scrollRectToVisible(textPane.modelToView(offset)); } catch (BadLocationException ex) { } }); textPane.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent evt) { searcher.search(word); } @Override public void removeUpdate(DocumentEvent evt) { searcher.search(word); } @Override public void changedUpdate(DocumentEvent evt) { } }); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(panel, "South"); f.add(new JScrollPane(textPane), "Center"); f.setSize(400, 400); f.setVisible(true); }