public class TextAreaFrame extends JFrame
1: // Fig. 22.1: TextAreaFrame.java
2: // Copying selected text from one textarea to another.
3: import java.awt.event.ActionListener;
4: import java.awt.event.ActionEvent;
5: import javax.swing.Box;
6: import javax.swing.JFrame;
7: import javax.swing.JTextArea;
8: import javax.swing.JButton;
9: import javax.swing.JScrollPane;
10:
11: public class TextAreaFrame extends JFrame
12: {
13: private JTextArea textArea1; // displays demo string
14: private JTextArea textArea2; // highlighted text is copied here
15: private JButton copyJButton; // initiates copying of text
16:
17: // no-argument constructor
18: public TextAreaFrame()
19: {
20: super( "TextArea Demo" );
21: Box box = Box.createHorizontalBox(); // create box
22: String demo = "This is a demo string to\n" +
23: "illustrate copying text\nfrom one textarea to \n" +
24: "another textarea using an\nexternal event\n";
25:
26: textArea1 = new JTextArea( demo, 10, 15 ); // create textarea1
27: box.add( new JScrollPane( textArea1 ) ); // add scrollpane
28:
29: copyJButton = new JButton( "Copy >>>" ); // create copy button
30: box.add( copyJButton ); // add copy button to box
31: copyJButton.addActionListener(
32:
33: new ActionListener() // anonymous inner class
34: {
35: // set text in textArea2 to selected text from textArea1
36: public void actionPerformed( ActionEvent event )
37: {
38: textArea2.setText( textArea1.getSelectedText() );
39: } // end method actionPerformed
40: } // end anonymous inner class
41: ); // end call to addActionListener
42:
43: textArea2 = new JTextArea( 10, 15 ); // create second textarea
44: textArea2.setEditable( false ); // disable editing
45: box.add( new JScrollPane( textArea2 ) ); // add scrollpane
46:
47: add( box ); // add box to frame
48: } // end TextAreaFrame constructor
49: } // end class TextAreaFrame
50:
51: /**************************************************************************
52: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
53: * Pearson Education, Inc. All Rights Reserved. *
54: * *
55: * DISCLAIMER: The authors and publisher of this book have used their *
56: * best efforts in preparing the book. These efforts include the *
57: * development, research, and testing of the theories and programs *
58: * to determine their effectiveness. The authors and publisher make *
59: * no warranty of any kind, expressed or implied, with regard to these *
60: * programs or to the documentation contained in these books. The authors *
61: * and publisher shall not be liable in any event for incidental or *
62: * consequential damages in connection with, or arising out of, the *
63: * furnishing, performance, or use of these programs. *
64: *************************************************************************/