The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.text  [49 examples] > Styles  [9 examples]

e1006. Highlighting Words in a JTextComponent

This example demonstrates how to highlight a range of characters in a text component using highlights. The example uses a text component's highlighter to create and manage the highlights. Since one of the highlights in the highlighter is the selection, we must be careful when removing highlights not to remove the selection.

The way in which we identify our highlights is to create the highlight using a private painter. So when removing highlights from the highlighter, we remove only the ones whose painter is an instance of the private painter.

If many different highlight styles are required, it is necessary to use styles with a JTextPane. See e1007 Setting the Font and Color of Text in a JTextPane Using Styles for more details.

    JTextArea textComp = new JTextArea();
    
    // Highlight the occurrences of the word "public"
    highlight(textComp, "public");
    
    // Creates highlights around all occurrences of pattern in textComp
    public void highlight(JTextComponent textComp, String pattern) {
        // First remove all old highlights
        removeHighlights(textComp);
    
        try {
            Highlighter hilite = textComp.getHighlighter();
            Document doc = textComp.getDocument();
            String text = doc.getText(0, doc.getLength());
            int pos = 0;
    
            // Search for pattern
            while ((pos = text.indexOf(pattern, pos)) >= 0) {
                // Create highlighter using private painter and apply around pattern
                hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
                pos += pattern.length();
            }
        } catch (BadLocationException e) {
        }
    }
    
    // Removes only our private highlights
    public void removeHighlights(JTextComponent textComp) {
        Highlighter hilite = textComp.getHighlighter();
        Highlighter.Highlight[] hilites = hilite.getHighlights();
    
        for (int i=0; i<hilites.length; i++) {
            if (hilites[i].getPainter() instanceof MyHighlightPainter) {
                hilite.removeHighlight(hilites[i]);
            }
        }
    }
    
    // An instance of the private subclass of the default highlight painter
    Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);
    
    // A private subclass of the default highlight painter
    class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
        public MyHighlightPainter(Color color) {
            super(color);
        }
    }

 Related Examples
e1007. Setting the Font and Color of Text in a JTextPane Using Styles
e1008. Sharing Styles Between JTextPanes
e1009. Listing the Styles Associated with a JTextPane
e1010. Listing the Attributes in a Style
e1011. Using a Popup to Apply Styles to a JTextPane
e1012. Retaining the Logical Style When Setting a New Paragraph Style
e1013. Automatically Updating Styled Text When a Style Is Updated
e1014. Determining If a Style Attribute Applies to a Character or the Paragraph

See also: Actions and Key Bindings    Caret and Selection    Events    JEditorPane    JFormattedTextField    JTextArea    JTextComponent    JTextField    JTextPane   


© 2002 Addison-Wesley.