Example usage for javax.swing.text JTextComponent getHighlighter

List of usage examples for javax.swing.text JTextComponent getHighlighter

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent getHighlighter.

Prototype

public Highlighter getHighlighter() 

Source Link

Document

Fetches the object responsible for making highlights.

Usage

From source file:Main.java

public static void removeHighlights(JTextComponent textComp) {
    Highlighter hilite = textComp.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();

    for (int i = 0; i < hilites.length; i++) {
        hilite.removeHighlight(hilites[i]);
    }/*from   w  ww . j a v a  2 s  . c  o  m*/
}

From source file:Main.java

public static void highlight(JTextComponent textComp) {
    try {// w ww.  java 2  s. c om
        Highlighter hilite = textComp.getHighlighter();
        Document doc = textComp.getDocument();
        hilite.addHighlight(3, 5, new MyHighlightPainter(Color.red));
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static 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]);
        }/*from  w  w  w. j  av a  2 s.co  m*/
    }
}

From source file:Main.java

public static 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 Highlighter) {
            hilite.removeHighlight(hilites[i]);
        }/*from w ww.  j  a va 2  s .  co m*/
    }
}

From source file:Main.java

public static void highlight(JTextComponent textComp, String pattern) throws Exception {
    removeHighlights(textComp);//  w ww.  j  a  v a  2 s  .  co  m

    Highlighter hilite = textComp.getHighlighter();
    Document doc = textComp.getDocument();
    String text = doc.getText(0, doc.getLength());
    int pos = 0;

    while ((pos = text.indexOf(pattern, pos)) >= 0) {
        hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
        pos += pattern.length();
    }

}

From source file:com.prodigy4440.view.MainJFrame.java

public static void highlightText(JTextComponent component, String s) {
    try {//from   w  ww.j a  va 2s  . com
        Highlighter highlighter = component.getHighlighter();
        String text = component.getText();
        String line = null;
        int start = 0;
        int end;
        int totalLines = ((JTextArea) component).getLineCount();
        for (int i = 0; i < totalLines; i++) {
            if (i == 5) {
                start = ((JTextArea) component).getLineOfOffset(i);
                end = ((JTextArea) component).getLineEndOffset(i);
                line = text.substring(start, end);
            }

        }

        int pos = start;
        if ((pos = text.indexOf(s, pos)) >= start) {
            DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(
                    Color.BLUE);
            highlighter.addHighlight(pos, pos + s.length(), highlightPainter);
        }
    } catch (BadLocationException ble) {

    }
}

From source file:ch.zhaw.iamp.rct.ui.GrammarWindow.java

private void highlightErrorLine(int lineIndex, JTextComponent component) {
    String text = component.getText() + "\n";
    int startPosition = 0;
    int endPosition = 0;

    for (int i = 0; i <= lineIndex; i++) {
        startPosition = endPosition > 0 ? endPosition + 1 : 0;
        endPosition = text.indexOf('\n', startPosition);
    }//  w w w  .  j a  va  2 s  .  c o  m

    try {
        DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(
                Color.RED);
        component.getHighlighter().addHighlight(startPosition, endPosition, highlightPainter);
    } catch (BadLocationException ex) {
        System.out.println("Unable to highlight line with index " + lineIndex + ": " + ex.getMessage());
    }
}