Java Swing How to - Replace text in text area








Question

We would like to know how to replace text in text area.

Answer

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/* w  w w. j a v  a2  s . com*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class TextEditFrame extends JFrame {

  private JTextArea textArea = new JTextArea(8, 40);

  private JScrollPane scrollPane = new JScrollPane(textArea);

  private JTextField fromField = new JTextField(8);

  private JTextField toField = new JTextField(8);

  public TextEditFrame() {
    setTitle("TextEditTest");
    setSize(500, 300);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = getContentPane();

    JPanel panel = new JPanel();

    JButton replaceButton = new JButton("Replace");
    panel.add(replaceButton);
    replaceButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        String from = fromField.getText();
        int start = textArea.getText().indexOf(from);
        if (start >= 0 && from.length() > 0)
          textArea.replaceRange(toField.getText(), start, start
              + from.length());
      }
    });

    panel.add(fromField);

    panel.add(new JLabel("with"));

    panel.add(toField);

    contentPane.add(panel, "South");
    contentPane.add(scrollPane, "Center");
  }

  public static void main(String[] args) {
    JFrame f = new TextEditFrame();
    f.show();
  }
}