Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTextPane highlight text

Can I highlight some text into a JTextPane starting from a value and ending from another value like the following but with the yellow color?

"" JTextPane highlight text ""

Thanks.

like image 366
xdevel2000 Avatar asked Apr 15 '11 08:04

xdevel2000


3 Answers

As often there are several possibilities, depending on what you really mean by "highlight":-)

Highlight by changing any style attributes of arbitrary text parts on the document level, something like

    SimpleAttributeSet sas = new SimpleAttributeSet();
    StyleConstants.setForeground(sas, Color.YELLOW);
    doc.setCharacterAttributes(start, length, sas, false);

Highlight via a Highlighter on the textPane level:

    DefaultHighlighter.DefaultHighlightPainter highlightPainter = 
        new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
    textPane.getHighlighter().addHighlight(startPos, endPos, 
            highlightPainter);
like image 142
kleopatra Avatar answered Nov 13 '22 20:11

kleopatra


https://web.archive.org/web/20120530071821/http://www.exampledepot.com/egs/javax.swing.text/style_HiliteWords.html

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
        // see I have updated now its not case sensitive 
        while ((pos = text.toUpperCase().indexOf(pattern.toUpperCase(), 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);
    }
}
like image 31
Makky Avatar answered Nov 13 '22 21:11

Makky


Yes you can via the functions setSelectionStart and setSelectionEnd from JTextComponent which JTextPane inherits from.

see javadoc of JTextComponent.setSelectionStart

like image 4
clamp Avatar answered Nov 13 '22 22:11

clamp