Java Code Examples for javax.swing.text.Highlighter#removeHighlight()

The following examples show how to use javax.swing.text.Highlighter#removeHighlight() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CaretFloatingPointAPITest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void updateSelection() {
    Highlighter h = component.getHighlighter();
    if (h != null) {
        int p0 = Math.min(dot, mark);
        int p1 = Math.max(dot, mark);

        if (p0 == p1 || !selectionVisible) {
            if (selectionTag != null) {
                h.removeHighlight(selectionTag);
                selectionTag = null;
            }
        } else {
            try {
                if (selectionTag != null) {
                    h.changeHighlight(selectionTag, p0, p1);
                } else {
                    Highlighter.HighlightPainter p = getSelectionPainter();
                    selectionTag = h.addHighlight(p0, p1, p);
                }
            } catch (BadLocationException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
 
Example 2
Source File: CaretFloatingPointAPITest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void updateSelection() {
    Highlighter h = component.getHighlighter();
    if (h != null) {
        int p0 = Math.min(dot, mark);
        int p1 = Math.max(dot, mark);

        if (p0 == p1 || !selectionVisible) {
            if (selectionTag != null) {
                h.removeHighlight(selectionTag);
                selectionTag = null;
            }
        } else {
            try {
                if (selectionTag != null) {
                    h.changeHighlight(selectionTag, p0, p1);
                } else {
                    Highlighter.HighlightPainter p = getSelectionPainter();
                    selectionTag = h.addHighlight(p0, p1, p);
                }
            } catch (BadLocationException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
 
Example 3
Source File: Markers.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes only our private highlights
 * This is public so that we can remove the highlights when the editorKit
 * is unregistered.  SimpleMarker can be null, in which case all instances of
 * our Markers are removed.
 * @param component the text component whose markers are to be removed
 * @param marker the SimpleMarker to remove
 */
public static void removeMarkers(JTextComponent component, SimpleMarker marker) {
    Highlighter hilite = component.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();

    for (int i = 0; i < hilites.length; i++) {
        if (hilites[i].getPainter() instanceof SimpleMarker) {
            SimpleMarker hMarker = (SimpleMarker) hilites[i].getPainter();
            if (marker == null || hMarker.equals(marker)) {
                hilite.removeHighlight(hilites[i]);
            }
        }
    }
}
 
Example 4
Source File: MyMarkers.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes only our private highlights This is public so that we can remove
 * the highlights when the editorKit is unregistered. SimpleMarker can be
 * null, in which case all instances of our Markers are removed.
 *
 * @param component the text component whose markers are to be removed
 * @param marker the SimpleMarker to remove
 */
public static void removeMarkers(JTextComponent component, Highlighter.HighlightPainter marker) {
    Highlighter hilite = component.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();

    for (int i = 0; i < hilites.length; i++) {
        Highlighter.HighlightPainter hMarker = hilites[i].getPainter();
        if (marker == null || hMarker.equals(marker)) {
            hilite.removeHighlight(hilites[i]);
        }
    }
}
 
Example 5
Source File: Markers.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes only our private highlights
 * This is public so that we can remove the highlights when the editorKit
 * is unregistered.  SimpleMarker can be null, in which case all instances of
 * our Markers are removed.
 * @param component the text component whose markers are to be removed
 * @param marker the SimpleMarker to remove
 */
public static void removeMarkers(JTextComponent component, SimpleMarker marker) {
    Highlighter hilite = component.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();

    for (int i = 0; i < hilites.length; i++) {
        if (hilites[i].getPainter() instanceof SimpleMarker) {
            SimpleMarker hMarker = (SimpleMarker) hilites[i].getPainter();
            if (marker == null || hMarker.equals(marker)) {
                hilite.removeHighlight(hilites[i]);
            }
        }
    }
}
 
Example 6
Source File: ColorHighlighter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public int highlight(String word) {        
    Highlighter highlighter = comp.getHighlighter();

    // remove old highlight before applying new one
    for (Highlighter.Highlight h : highlighter.getHighlights()) {
        if (h.getPainter() instanceof ColorHighlightPainter) {
            highlighter.removeHighlight(h);
        }
    }
    
    if (word == null || word.equals("")) {
        return -1;
    }

    // search for the word, case insentitive
    String content = null;
    try {
        Document d = comp.getDocument();
        content = d.getText(0, d.getLength()).toLowerCase();
    } catch (BadLocationException e) {
        return -1;
    }

    word = word.toLowerCase();
    int lastIndex = 0;
    int firstOffset = -1;
    int wordSize = word.length();

    while ((lastIndex = content.indexOf(word, lastIndex)) != -1) {
        int endIndex = lastIndex + wordSize;
        try {
            highlighter.addHighlight(lastIndex, endIndex, painter);
        } catch (BadLocationException ex) {
            // ignore
        }
        if (firstOffset == -1) {
            firstOffset = lastIndex;
        }
        lastIndex = endIndex;
    }

    return firstOffset;
}
 
Example 7
Source File: HTMLTextAreaSearchUtils.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
void clearHighlightedResults(Highlighter hl) {
    for (Object highlight : highlights) hl.removeHighlight(highlight);
    
    highlights.clear();
}