Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well I had to do some assumptions on your variable names and I hope I did it right. Basically your code worked for me but I didn't understand the first line:</p> <pre><code>int index = text.indexOf(myWord); </code></pre> <p>I think it should be:</p> <pre><code>int index = text.indexOf(pattern); </code></pre> <p>Anyway, I wrote this code and it worked for me:</p> <pre><code>String pattern = "&lt;aa&gt;"; String text = textArea.getText(); int index = text.indexOf(pattern); while(index &gt;= 0){ try { hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter); index = text.indexOf(pattern, index + pattern.length()); } catch (BadLocationException ex) { ex.printStackTrace(); } } </code></pre> <hr> <p><strong>Update</strong></p> <blockquote> <p>Look when you click on the end of marked text and start typing the text will be also marked- it is a my bug.</p> </blockquote> <p>Sorry I didn't see that before. I don't think you can <em>stop</em> a highlighter but you can make it using a <code>CaretListener</code> and doing the stuff there. This way if you input a new string that matches your pattern it will be highlighted too:</p> <pre><code>textArea.addCaretListener(new CaretListener() { @Override public void caretUpdate(CaretEvent e) { if(e.getMark() == e.getDot()){ Highlighter hl = textArea.getHighlighter(); hl.removeAllHighlights(); String pattern = "&lt;aa&gt;"; String text = textArea.getText(); int index = text.indexOf(pattern); while(index &gt;= 0){ try { Object o = hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter); index = text.indexOf(pattern, index + pattern.length()); } catch (BadLocationException ex) { ex.printStackTrace(); } } } } }); </code></pre> <p>Note <code>e.getMark() == e.getDot()</code> means "there's no text selection" based on <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/event/CaretEvent.html#getDot%28%29" rel="noreferrer">CaretEvent.getDot()</a> and <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/event/CaretEvent.html#getMark%28%29" rel="noreferrer">CaretEvent.getMark()</a> javadoc. Otherwise you shouldn't highlight anything but selected text.</p> <p><strong>Screenshot</strong></p> <p><img src="https://i.stack.imgur.com/t8GIR.jpg" alt="enter image description here"></p> <p><strong>Updated SSCCE</strong></p> <p>Here's a complete <a href="http://sscce.org/" rel="noreferrer">SSCCE</a> to test it:</p> <pre><code>import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import javax.swing.event.CaretEvent; import javax.swing.event.CaretListener; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultHighlighter; import javax.swing.text.Highlighter; public class Demo { private void initGUI(){ final JTextArea textArea = new JTextArea(); textArea.addCaretListener(new CaretListener() { @Override public void caretUpdate(CaretEvent e) { if(e.getMark() == e.getDot()){ Highlighter hl = textArea.getHighlighter(); hl.removeAllHighlights(); String pattern = "&lt;aa&gt;"; String text = textArea.getText(); int index = text.indexOf(pattern); while(index &gt;= 0){ try { Object o = hl.addHighlight(index, index + pattern.length(), DefaultHighlighter.DefaultPainter); index = text.indexOf(pattern, index + pattern.length()); } catch (BadLocationException ex) { ex.printStackTrace(); } } } } }); JScrollPane scrollPane = new JScrollPane(textArea); scrollPane.setPreferredSize(new Dimension(300,200)); JPanel content = new JPanel(new FlowLayout()); content.add(scrollPane); JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setContentPane(content); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Demo().initGUI(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload