List of usage examples for javax.swing.text Highlighter addHighlight
public Object addHighlight(int p0, int p1, HighlightPainter p) throws BadLocationException;
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();//from w w w . j av 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) h.addHighlight(j, j + 1, DefaultHighlighter.DefaultPainter); } frame.pack(); frame.setVisible(true); }
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();/* w w w .j a v a 2 s. 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 highlight(JTextComponent textComp) { try {/*from ww w .j a va2 s. co m*/ 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 highlight(JTextComponent textComp, String pattern) throws Exception { removeHighlights(textComp);// w w w. j a v a2s .c o 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: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 .j a v a 2 s. c o 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();// w w w.j ava2 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:com.prodigy4440.view.MainJFrame.java
public static void highlightText(JTextComponent component, String s) { try {// w ww. java2s . c o m 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:net.sf.jabref.gui.fieldeditors.JTextAreaWithHighlighting.java
/** * Highlight words in the Textarea// www . j ava 2s .com * * @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:hr.fer.zemris.vhdllab.applets.texteditor.TextEditor.java
@Override public void highlightLine(int line) { int caret = textPane.getCaretPosition(); Highlighter h = textPane.getHighlighter(); h.removeAllHighlights();//www . j av a 2 s. c om String content = textPane.getText(); textPane.setCaretPosition(caret); int pos = 0; line--; while (line > 0) { pos = content.indexOf('\n', pos) + 1; line--; } int last = content.indexOf('\n', pos) + 1; if (last == 0) { last = content.length(); } try { highlighted = h.addHighlight(pos, last, new DefaultHighlighter.DefaultHighlightPainter(new Color(180, 210, 238))); } catch (BadLocationException e) { e.printStackTrace(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); JOptionPane.showMessageDialog(null, sw.toString()); } }
From source file:Main.java
public int search(String word) { int firstOffset = -1; Highlighter highlighter = comp.getHighlighter(); Highlighter.Highlight[] highlights = highlighter.getHighlights(); for (int i = 0; i < highlights.length; i++) { Highlighter.Highlight h = highlights[i]; if (h.getPainter() instanceof UnderlineHighlightPainter) { highlighter.removeHighlight(h); }//from w ww .j a v a 2s .com } if (word == null || word.equals("")) { return -1; } 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 wordSize = word.length(); while ((lastIndex = content.indexOf(word, lastIndex)) != -1) { int endIndex = lastIndex + wordSize; try { highlighter.addHighlight(lastIndex, endIndex, painter); } catch (BadLocationException e) { } if (firstOffset == -1) { firstOffset = lastIndex; } lastIndex = endIndex; } return firstOffset; }