Example usage for javax.swing.text Highlighter removeAllHighlights

List of usage examples for javax.swing.text Highlighter removeAllHighlights

Introduction

In this page you can find the example usage for javax.swing.text Highlighter removeAllHighlights.

Prototype

public void removeAllHighlights();

Source Link

Document

Removes all highlights this highlighter is responsible for.

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();
    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  ww  w . j  ava 2 s.c  om*/
    }
}

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();
    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);
    }/*  w  ww.j a va 2s. c o m*/
    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();
    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) {
            }/* w  w  w .  ja  v a 2 s  .  c o m*/
    }
    this.getContentPane().add(area);

}

From source file:MultiHighlight.java

public void actionPerformed(ActionEvent e) {
    Highlighter h = comp.getHighlighter();
    h.removeAllHighlights();
    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) {
            }/* www  . ja v  a  2s .  c om*/
    }
}

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

/**
 * Highlight words in the Textarea/* w w  w  .j av a 2 s  .  c o  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:logdruid.ui.table.StatRecordingEditorTable.java

public void FixValues() {
    String patternString = "";
    Matcher matcher;//from  w  ww.  j  a  va2s  . co m
    PatternCache patternCache = new PatternCache();
    Iterator it = data.iterator();
    Object[] obj;

    while (it.hasNext()) {
        obj = (Object[]) it.next();
        String stBefore = (String) obj[1];
        String stType = (String) obj[2];
        String stAfter = (String) obj[3];
        logger.info("stType: " + stType);
        if (stType.equals("date") && rep.getDateFormat(recording.getDateFormatID()).getPattern() != null) {
            patternString += stBefore + "(" + rep.getDateFormat(recording.getDateFormatID()).getPattern() + ")"
                    + stAfter;
            logger.info("getTypeString(stType) getPattern -: "
                    + rep.getDateFormat(recording.getDateFormatID()).getPattern());
            logger.info("getTypeString(stType) getDateFormat -: "
                    + rep.getDateFormat(recording.getDateFormatID()).getDateFormat());
        } else {
            patternString += stBefore + "(" + DataMiner.getTypeString(stType) + ")" + stAfter;
            logger.info("getTypeString(stType) -: " + DataMiner.getTypeString(stType));
        }
    }

    try {
        logger.info("theLine: " + examplePane.getText());
        logger.info("patternString: " + patternString);

        Highlighter h = examplePane.getHighlighter();
        h.removeAllHighlights();
        int currIndex = 0;

        String[] lines = examplePane.getText().split(System.getProperty("line.separator"));
        if (lines.length >= 1) {
            for (int i = 0; i < lines.length; i++) {
                matcher = patternCache.getPattern(patternString).matcher(lines[i]);
                if (matcher.find()) {
                    // int currIndex = 0;
                    // doc.insertString(doc.getLength(),line+"\n", null);

                    for (int i2 = 1; i2 <= matcher.groupCount(); i2++) {
                        model.setValueAt(matcher.group(i2), i2 - 1, 5);
                        h.addHighlight(currIndex + matcher.start(i2), +currIndex + matcher.end(i2),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE));
                    }

                }
                currIndex += lines[i].length() + 1;
            }
        }

    } catch (Exception e1) {
        e1.printStackTrace();
        // System.exit(1);
    }

}

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();
    String content = textPane.getText();
    textPane.setCaretPosition(caret);//from  w  w w .j  av  a 2  s. co m

    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:logdruid.ui.table.EventRecordingEditorTable.java

public void FixValues() {
    String patternString = "";
    Matcher matcher = null;//from w w  w .  j a  v  a  2 s. c om
    PatternCache patternCache = new PatternCache();
    Iterator it = data.iterator();
    Object[] obj;

    while (it.hasNext()) {
        obj = (Object[]) it.next();
        String stBefore = (String) obj[1];
        String stType = (String) obj[2];
        String stAfter = (String) obj[4];
        logger.info("stType: " + stType);
        if (stType.equals("date") && rep.getDateFormat(recording.getDateFormatID()).getPattern() != null) {
            patternString += stBefore + "(" + rep.getDateFormat(recording.getDateFormatID()).getPattern() + ")"
                    + stAfter;
            logger.info("getTypeString(stType) getPattern -: "
                    + rep.getDateFormat(recording.getDateFormatID()).getPattern());
            logger.info("getTypeString(stType) getDateFormat -: "
                    + rep.getDateFormat(recording.getDateFormatID()).getDateFormat());
        } else {
            patternString += stBefore + "(" + DataMiner.getTypeString(stType) + ")" + stAfter;
            logger.info("getTypeString(stType) -: " + DataMiner.getTypeString(stType));
        }
    }

    try {
        logger.info("theLine: " + examplePane.getText());
        logger.info("patternString: " + patternString);
        Highlighter h = examplePane.getHighlighter();
        h.removeAllHighlights();
        int currIndex = 0;

        String[] lines = examplePane.getText().split(System.getProperty("line.separator"));
        if (lines.length >= 1) {
            for (int i = 0; i < lines.length; i++) {
                matcher = patternCache.getPattern(patternString).matcher(lines[i]);
                if (matcher.find()) {
                    // int currIndex = 0;
                    // doc.insertString(doc.getLength(),line+"\n", null);

                    for (int i2 = 1; i2 <= matcher.groupCount(); i2++) {
                        model.setValueAt(matcher.group(i2), i2 - 1, 6);
                        h.addHighlight(currIndex + matcher.start(i2), +currIndex + matcher.end(i2),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE));
                    }
                }
                logger.info("currIndex: " + currIndex + "matcher.end(i2): " + lines[i].length() + ",l: "
                        + lines[i]);
                currIndex += lines[i].length() + 1;
            }
        }

    } catch (Exception e1) {
        e1.printStackTrace();
        // System.exit(1);
    }

}

From source file:com.petersoft.advancedswing.enhancedtextarea.EnhancedTextArea.java

private void jSearchTextFieldKeyReleased(KeyEvent evt) {
    String text = jTextArea.getText().toLowerCase();
    String searchPattern = jSearchTextField.getText().toLowerCase();

    if (evt != null && evt.getKeyCode() == 10) {
        int caretPosition = jTextArea.getCaretPosition();
        boolean found = false;
        for (int j = caretPosition + 1; j < text.length() - searchPattern.length(); j += 1) {
            if (searchPattern.equals(text.substring(j, j + searchPattern.length()))) {
                jTextArea.setCaretPosition(j);
                found = true;//from w w  w .  j a  va2 s .  co  m
                break;
            }
        }
        if (!found) {
            for (int j = 0; j < caretPosition; j++) {
                if (searchPattern.equals(text.substring(j, j + searchPattern.length()))) {
                    jTextArea.setCaretPosition(j);
                    break;
                }
            }
        }
    }

    if (searchPattern.length() > 0) {
        Highlighter h = jTextArea.getHighlighter();
        DefaultHighlightPainter painter = new DefaultHighlightPainter(Color.YELLOW);
        DefaultHighlightPainter painter2 = new DefaultHighlightPainter(Color.RED);
        h.removeAllHighlights();

        int count = 0;
        boolean isCurrent = false;
        for (int j = 0; j < text.length(); j += 1) {
            if (j < text.length() - searchPattern.length()
                    && searchPattern.equals(text.substring(j, j + searchPattern.length()))) {
                count++;
                try {
                    if (j >= jTextArea.getCaretPosition() && isCurrent == false) {
                        h.addHighlight(j, j + searchPattern.length(), painter2);
                        isCurrent = true;
                    } else {
                        h.addHighlight(j, j + searchPattern.length(), painter);
                    }
                } catch (BadLocationException ble) {
                }
            }
        }
        jSearchLabel.setText("Match:" + count);
    } else {
        jSearchLabel.setText("");
        Highlighter h = jTextArea.getHighlighter();
        h.removeAllHighlights();
    }
}

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

public final void initComponents() {

    List<Image> icons = new LinkedList<>();
    icons.add(new ImageIcon(getClass().getResource("/com/prodigy4440/ited16x16.png")).getImage());
    icons.add(new ImageIcon(getClass().getResource("/com/prodigy4440/ited32x32.png")).getImage());
    icons.add(new ImageIcon(getClass().getResource("/com/prodigy4440/ited48x48.png")).getImage());
    icons.add(new ImageIcon(getClass().getResource("/com/prodigy4440/ited72x72.png")).getImage());

    this.setIconImages(icons);

    ActionHandler actionHandler = new ActionHandler(this);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(620, 520);
    this.setLocationRelativeTo(null);
    this.setTitle("Untitled Document- IgboTextEditor");
    southJPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    SoftBevelBorder sbb = new SoftBevelBorder(SoftBevelBorder.LOWERED);
    southJPanel.setBorder(sbb);/*from  w  ww.  java  2  s . c  om*/
    menuBar = new JMenuBar();

    fileJMenu = new JMenu("File");
    fileJMenu.setMnemonic('F');
    editJMenu = new JMenu("Edit");
    editJMenu.setMnemonic('E');
    formatJMenu = new JMenu("Format");
    formatJMenu.setMnemonic('A');
    viewJMenu = new JMenu("View");
    viewJMenu.setMnemonic('V');
    helpJMenu = new JMenu("Help");
    helpJMenu.setMnemonic('H');

    newDocumentJMenuItem = new JMenuItem("New");
    newDocumentJMenuItem.addActionListener(actionHandler);
    newDocumentJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
    openJMenuItem = new JMenuItem("Open");
    openJMenuItem.addActionListener(actionHandler);
    openJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
    saveJMenuItem = new JMenuItem("Save");
    saveJMenuItem.addActionListener(actionHandler);
    saveJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
    printJMenuItem = new JMenuItem("Print");
    printJMenuItem.addActionListener(actionHandler);
    printJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK));
    exitJMenuItem = new JMenuItem("Exit");
    exitJMenuItem.addActionListener(actionHandler);

    undoJMenuItem = new JMenuItem("Undo");
    undoJMenuItem.addActionListener(actionHandler);
    undoJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK));
    redoJMenuItem = new JMenuItem("Redo");
    redoJMenuItem.addActionListener(actionHandler);
    redoJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK));
    copyJMenuItem = new JMenuItem("Copy");
    copyJMenuItem.addActionListener(actionHandler);
    copyJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
    cutJMenuItem = new JMenuItem("Cut");
    cutJMenuItem.addActionListener(actionHandler);
    cutJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
    pasteJMenuItem = new JMenuItem("Paste");
    pasteJMenuItem.addActionListener(actionHandler);
    pasteJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
    deleteJMenuItem = new JMenuItem("Delete");
    deleteJMenuItem.addActionListener(actionHandler);
    deleteJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK));
    selectAllJMenuItem = new JMenuItem("Select All");
    selectAllJMenuItem.addActionListener(actionHandler);
    selectAllJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK));
    findJMenuItem = new JMenuItem("Find");
    findJMenuItem.addActionListener(actionHandler);
    findJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK));
    replaceJMenuItem = new JMenuItem("Replace");
    replaceJMenuItem.addActionListener(actionHandler);
    replaceJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, Event.CTRL_MASK));

    wordWrapJCheckBoxMenuItem = new JCheckBoxMenuItem("Word Wrap");
    wordWrapJCheckBoxMenuItem.addActionListener(actionHandler);
    wordWrapJCheckBoxMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, Event.CTRL_MASK));
    fontJMenuItem = new JMenuItem("Font");
    fontJMenuItem.addActionListener(actionHandler);
    colorJMenuItem = new JMenuItem("Color");
    colorJMenuItem.addActionListener(actionHandler);

    statusBarJCheckBoxMenuItem = new JCheckBoxMenuItem("Status Bar");
    statusBarJCheckBoxMenuItem.addActionListener(actionHandler);
    statusBarJCheckBoxMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.ALT_MASK));

    helpJMenuItem = new JMenuItem("Help");
    helpJMenuItem.addActionListener(actionHandler);
    helpJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, Event.CTRL_MASK));
    aboutJMenuItem = new JMenuItem("About");
    aboutJMenuItem.addActionListener(actionHandler);

    statusJLabel = new JLabel("Status:");

    //Main text area setup
    textArea = new JTextArea();
    undoManager = new UndoManager();
    wordSearcher = new WordSearcher(textArea);
    textArea.setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.WHITE));
    document = textArea.getDocument();
    document.addUndoableEditListener(new UndoableEditListener() {
        @Override
        public void undoableEditHappened(UndoableEditEvent e) {
            undoManager.addEdit(e.getEdit());
        }
    });

    font = new Font("Tahoma", Font.PLAIN, 16);
    textArea.setFont(font);
    color = Color.BLUE;
    textArea.setForeground(color);

    undoManager = new UndoManager();

    fileJMenu.add(newDocumentJMenuItem);
    fileJMenu.addSeparator();
    fileJMenu.add(openJMenuItem);
    fileJMenu.add(saveJMenuItem);
    fileJMenu.addSeparator();
    fileJMenu.add(printJMenuItem);
    fileJMenu.addSeparator();
    fileJMenu.add(exitJMenuItem);

    editJMenu.add(undoJMenuItem);
    editJMenu.add(redoJMenuItem);
    editJMenu.addSeparator();
    editJMenu.add(copyJMenuItem);
    editJMenu.add(cutJMenuItem);
    editJMenu.add(pasteJMenuItem);
    editJMenu.addSeparator();
    editJMenu.add(deleteJMenuItem);
    editJMenu.add(selectAllJMenuItem);
    editJMenu.addSeparator();
    editJMenu.add(findJMenuItem);
    editJMenu.add(replaceJMenuItem);

    formatJMenu.add(wordWrapJCheckBoxMenuItem);
    formatJMenu.add(fontJMenuItem);
    formatJMenu.add(colorJMenuItem);

    viewJMenu.add(statusBarJCheckBoxMenuItem);

    helpJMenu.add(helpJMenuItem);
    helpJMenu.add(aboutJMenuItem);

    menuBar.add(fileJMenu);
    menuBar.add(editJMenu);
    menuBar.add(formatJMenu);
    menuBar.add(viewJMenu);
    menuBar.add(helpJMenu);

    southJPanel.setVisible(false);
    southJPanel.add(statusJLabel);
    //JScrollPane setup
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    //setting uo the Jframe
    this.setJMenuBar(menuBar);
    this.add(scrollPane, BorderLayout.CENTER);
    this.add(southJPanel, BorderLayout.SOUTH);
    textArea.addMouseListener(new MouseInputListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            Highlighter h = textArea.getHighlighter();
            h.removeAllHighlights();
        }

        @Override
        public void mousePressed(MouseEvent e) {
            Highlighter h = textArea.getHighlighter();
            h.removeAllHighlights();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseDragged(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        }
    });

    textArea.addKeyListener(new IgboKeyListener(textArea));

}