Example usage for javax.swing.text DefaultHighlighter DefaultPainter

List of usage examples for javax.swing.text DefaultHighlighter DefaultPainter

Introduction

In this page you can find the example usage for javax.swing.text DefaultHighlighter DefaultPainter.

Prototype

LayeredHighlighter.LayerPainter DefaultPainter

To view the source code for javax.swing.text DefaultHighlighter DefaultPainter.

Click Source Link

Document

Default implementation of LayeredHighlighter.LayerPainter that can be used for painting highlights.

Usage

From source file:Main.java

public static void main(String args[]) {
    JTextArea area = new JTextArea(5, 20);
    area.setText("this is a test.");
    String charsToHighlight = "aeiouAEIOU";
    Highlighter h = area.getHighlighter();
    h.removeAllHighlights();/*  ww w  .  j  a  va 2s .  c  om*/
    String text = area.getText().toUpperCase();
    for (int i = 0; i < text.length(); i += 1) {
        char ch = text.charAt(i);
        if (charsToHighlight.indexOf(ch) >= 0)
            try {
                h.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);
            } catch (Exception ble) {
            }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("MultiHighlight");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea comp = new JTextArea(5, 20);
    comp.setText("this is a test");
    frame.getContentPane().add(new JScrollPane(comp), BorderLayout.CENTER);

    String charsToHighlight = "a";
    Highlighter h = comp.getHighlighter();
    h.removeAllHighlights();/* w ww. java 2  s  .c om*/
    String text = comp.getText().toUpperCase();

    for (int j = 0; j < text.length(); j += 1) {
        char ch = text.charAt(j);
        if (charsToHighlight.indexOf(ch) >= 0)
            h.addHighlight(j, j + 1, DefaultHighlighter.DefaultPainter);
    }
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    JTextArea area = new JTextArea(5, 20);
    area.setText("this is a test.");
    String charsToHighlight = "aeiouAEIOU";
    Highlighter h = area.getHighlighter();
    h.removeAllHighlights();//from   w w w .  ja  v  a2  s .co  m
    String text = area.getText().toUpperCase();
    for (int i = 0; i < text.length(); i += 1) {
        char ch = text.charAt(i);
        if (charsToHighlight.indexOf(ch) >= 0)
            try {
                h.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);
            } catch (Exception ble) {
            }
    }
    this.getContentPane().add(area);

}

From source file:MultiHighlight.java

public void actionPerformed(ActionEvent e) {
    Highlighter h = comp.getHighlighter();
    h.removeAllHighlights();/*from ww w.  j  a v a2  s  . co m*/
    String text = comp.getText().toUpperCase();

    for (int j = 0; j < text.length(); j += 1) {
        char ch = text.charAt(j);
        if (charsToHighlight.indexOf(ch) >= 0)
            try {
                h.addHighlight(j, j + 1, DefaultHighlighter.DefaultPainter);
            } catch (BadLocationException ble) {
            }
    }
}

From source file:net.sf.jabref.gui.fieldeditors.JTextAreaWithHighlighting.java

/**
 * Highlight words in the Textarea/*from  www  . ja v a 2s. co m*/
 *
 * @param words to highlight
 */
private void highLight() {
    // highlight all characters that appear in charsToHighlight
    Highlighter highlighter = getHighlighter();
    highlighter.removeAllHighlights();

    if ((highlightPattern == null) || !highlightPattern.isPresent()) {
        return;
    }
    String content = getText();
    if (content.isEmpty()) {
        return;
    }

    highlightPattern.ifPresent(pattern -> {
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            try {
                highlighter.addHighlight(matcher.start(), matcher.end(), DefaultHighlighter.DefaultPainter);
            } catch (BadLocationException ble) {
                // should not occur if matcher works right
                LOGGER.warn("Highlighting not possible, bad location", ble);
            }
        }
    });

}

From source file:org.monkeys.gui.matcher.MatcherPanel.java

private void highlightMatches(final Collection<RegexMatch> matches) {
    this.clearHighlights();
    if (null == matches || matches.isEmpty()) {
        return;//from   w w  w .  ja  va2  s .c  o m
    }

    final Highlighter h = this.textArea.getHighlighter();
    final Highlighter.HighlightPainter style = DefaultHighlighter.DefaultPainter;
    for (final RegexMatch match : matches) {
        final int p0 = match.getStart();
        final int p1 = match.getEnd();
        try {
            h.addHighlight(p0, p1, style);
        } catch (final BadLocationException e) {
            MessageUtils.exceptionDialog("Unanble to highlight match [" + match + "].", e);
        }
    }
}