Source Code : Replace text in text area

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Replace text in text area

Replace text in text area
 
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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();
  }
}


           
         
  

Thank with us