Like this article? We recommend
Running It
We have a class, BracketMatcher, that listens to the caret. We just need to apply it to a JTextComponent of some sort, to get it running:
JEditorPane p = new JEditorPane(); p.addCaretListener(new BracketMatcher());
Put that in a JFrame, and it looks like Figures 3 and 4.
Figure 3 A highlighted document.
Figure 4 Incorrect matching.