import java.awt.*; import java.awt.event.*; import javax.swing.*; class TestTextArea extends JFrame{ /* Create pointers to the two text areas * and the button. */ private JTextArea left, right; private JButton copy; /* Constructor method sets up the JFrame */ public TestTextArea(){ /* Name of the JFrame */ super("Test of TextArea"); /* Get ContentPane and set layout */ Container pane = getContentPane(); pane.setLayout(new BorderLayout()); /* String for left JTextfield */ String demoString = "This is a demo string to" + "\nillustrate copying text from one" + "\nTextArea to another"; /* Set up left JTextArea */ left = new JTextArea(demoString,10,15); left.setFont(new Font("Sansserif",Font.PLAIN,24)); pane.add(new JScrollPane(left),BorderLayout.WEST); /* Set up button */ copy = new JButton("Copy >>"); copy.setFont(new Font("Sansserif",Font.PLAIN,24)); copy.addActionListener(new ActionListener(){ // anonymous inner class public void actionPerformed(ActionEvent e){ right.setText(left.getSelectedText()); } }); pane.add(copy,BorderLayout.CENTER); /* Set up right JTextArea */ right = new JTextArea(10,15); right.setFont(new Font("Sansserif",Font.PLAIN,24)); right.setEditable(false); pane.add(new JScrollPane(right),BorderLayout.EAST); /* Set size and make visible */ setSize(825,200); setVisible(true); } public static void main(String[] args){ TestTextArea tta = new TestTextArea(); tta.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){System.exit(0);} }); } }