Download as Eclipse project
Back to RSyntaxTextArea Home
RSyntaxTextArea has built-in support for search and replace operations, with all of the options you'd expect, including:
- Search forward or backward
- Match case
- Regular expressions
- Whole word
- Mark all
Any or all of these options can be used in conjunction with one another.
These actions (except for Mark All) are done with the SearchEngine class. This class contains self-explanatory methods for all your searching needs:
- find - Find the next/previous match with any of the above options.
- replace - Finds the next/previous match and replaces it with new text.
- replaceAll - Finds all matches and replaces them with the new text.
Finally, RTextArea (and thus RSyntaxTextArea) has a markAll method that uses the SearchEngine class to mark all instances of a given string in the editor.
Using the SearchEngine class makes implementing Find & Replace in an RSyntaxTextArea extremely easy. Below is a simple example of how it can be used.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.fife.ui.rtextarea.*; import org.fife.ui.rsyntaxtextarea.*; public class FindAndReplaceDemo extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private RSyntaxTextArea textArea; private JTextField searchField; private JCheckBox regexCB; private JCheckBox matchCaseCB; public FindAndReplaceDemo() { JPanel cp = new JPanel(new BorderLayout()); textArea = new RSyntaxTextArea(); textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); RTextScrollPane sp = new RTextScrollPane(textArea); cp.add(sp); // Create a toolbar with searching options. JToolBar toolBar = new JToolBar(); searchField = new JTextField(30); toolBar.add(searchField); JButton b = new JButton("Find Next"); b.setActionCommand("FindNext"); b.addActionListener(this); toolBar.add(b); b = new JButton("Find Previous"); b.setActionCommand("FindPrev"); b.addActionListener(this); toolBar.add(b); regexCB = new JCheckBox("Regex"); toolBar.add(regexCB); matchCaseCB = new JCheckBox("Match Case"); toolBar.add(matchCaseCB); cp.add(toolBar, BorderLayout.NORTH); setContentPane(cp); setTitle("RSyntaxTextArea 1.4 - Example 4 - Find and Replace Demo"); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if ("FindNext".equals(command)) { String text = searchField.getText(); if (text.length() == 0) { return; } boolean forward = true; boolean matchCase = matchCaseCB.isSelected(); boolean wholeWord = false; boolean regex = regexCB.isSelected(); boolean found = SearchEngine.find(textArea, text, forward, matchCase, wholeWord, regex); if (!found) { JOptionPane.showMessageDialog(this, "Text not found"); } } else if ("FindPrev".equals(command)) { String text = searchField.getText(); if (text.length() == 0) { return; } boolean forward = false; boolean matchCase = matchCaseCB.isSelected(); boolean wholeWord = false; boolean regex = regexCB.isSelected(); boolean found = SearchEngine.find(textArea, text, forward, matchCase, wholeWord, regex); if (!found) { JOptionPane.showMessageDialog(this, "Text not found"); } } } public static void main(String[] args) { // Start all Swing applications on the EDT. SwingUtilities.invokeLater(new Runnable() { public void run() { try { String laf = UIManager.getSystemLookAndFeelClassName(); UIManager.setLookAndFeel(laf); } catch (Exception e) {} FindAndReplaceDemo demo = new FindAndReplaceDemo(); demo.setVisible(true); demo.textArea.requestFocusInWindow(); } }); } }Save this file as FindAndReplaceDemo.java.
javac -classpath <path-to-jar>\rsyntaxtextarea.jar FindAndReplaceDemo.javawhere <path-to-jar> is the path to the rsyntaxtextarea.jar file. This should yield no errors or warnings, and on completion there should see a file named FindAndReplaceDemo.class in your current directory.
java -classpath <path-to-jar>\rsyntaxtextarea.jar;. FindAndReplaceDemo
A window should pop up, containing a text editor. At the top is a toolbar that allows you to search for text using several of the common options found in most editing applications.