Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No. You are not supposed to override the paintComponent() method. Instead, you should use <code>StyledDocument</code>. You should also delimit the words by your self.</p> <p>Here is the demo, which turns "public", "protected" and "private" to red when typing, just like a simple code editor:</p> <p><img src="https://i.stack.imgur.com/vP6aK.png" alt="enter image description here"></p> <pre><code>import javax.swing.*; import java.awt.*; import javax.swing.text.*; public class Test extends JFrame { private int findLastNonWordChar (String text, int index) { while (--index &gt;= 0) { if (String.valueOf(text.charAt(index)).matches("\\W")) { break; } } return index; } private int findFirstNonWordChar (String text, int index) { while (index &lt; text.length()) { if (String.valueOf(text.charAt(index)).matches("\\W")) { break; } index++; } return index; } public Test () { setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 400); setLocationRelativeTo(null); final StyleContext cont = StyleContext.getDefaultStyleContext(); final AttributeSet attr = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED); final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK); DefaultStyledDocument doc = new DefaultStyledDocument() { public void insertString (int offset, String str, AttributeSet a) throws BadLocationException { super.insertString(offset, str, a); String text = getText(0, getLength()); int before = findLastNonWordChar(text, offset); if (before &lt; 0) before = 0; int after = findFirstNonWordChar(text, offset + str.length()); int wordL = before; int wordR = before; while (wordR &lt;= after) { if (wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) { if (text.substring(wordL, wordR).matches("(\\W)*(private|public|protected)")) setCharacterAttributes(wordL, wordR - wordL, attr, false); else setCharacterAttributes(wordL, wordR - wordL, attrBlack, false); wordL = wordR; } wordR++; } } public void remove (int offs, int len) throws BadLocationException { super.remove(offs, len); String text = getText(0, getLength()); int before = findLastNonWordChar(text, offs); if (before &lt; 0) before = 0; int after = findFirstNonWordChar(text, offs); if (text.substring(before, after).matches("(\\W)*(private|public|protected)")) { setCharacterAttributes(before, after - before, attr, false); } else { setCharacterAttributes(before, after - before, attrBlack, false); } } }; JTextPane txt = new JTextPane(doc); txt.setText("public class Hi {}"); add(new JScrollPane(txt)); setVisible(true); } public static void main (String args[]) { new Test(); } } </code></pre> <p>The code is not so beautiful since I typed it quickly but it works. And I hope it will give you some hint.</p>
 

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