import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TextAreaDemo extends JFrame{
private JTextArea textArea1,textArea2;
private JButton copyButton;

//set up GUI
public TextAreaDemo(){
super("TextArea Demo");
Box box=Box.createHorizontalBox();

String string="This is a demo string to\n"+
"illustrate copying text\n"+
"from on TextArea to \n"+
"another TextArea using an\n"+"external event\n";

//set up textArea1
textArea1 = new JTextArea(string,10,15);
box.add(new JScrollPane(textArea1));

//set up copyButton 
copyButton = new JButton("Copy >>>");
copyButton.addActionListener(
//anonymous inner class to handle copyButton event
new ActionListener(){
//set text in textArea2 to selected
//text from textArea1
public void actionPerformed(ActionEvent event){
textArea2.setText(textArea1.getSelectedText());
}
}//end anonymous inner class 
);//end call to addActionListener
box.add(copyButton);

//set up textArea2
textArea2= new JTextArea(10,15);
textArea2.setEditalbe(false);
box.add(new JScrollPane(textArea2));

//add box to content pane
Container container=getContentPane();
container.add(box); //place in BorderLayout.CENTER

setSize(425,200);
setVisible(true);
}
//execute application
public static void main(String [] args){
TextAreaDemo application= new TextAreaDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}//end class TextAreaDemo