Download: TextAreaDemo.jar
TextAreaDemo.java
01: // TextAreaDemo.java 02: // Copying selected text from one text area to another. 03: import java.awt.*; 04: import java.awt.event.*; 05: import javax.swing.*; 06: 07: class TextAreaPanel extends JPanel { 08: private JTextArea t1, t2; 09: private JButton copy; 10: 11: public TextAreaPanel() 12: { 13: Box b = Box.createHorizontalBox(); 14: 15: String s = "This is a demo string to\n" + 16: "illustrate copying text\n" + 17: "from one TextArea to \n" + 18: "another TextArea using an\n"+ 19: "external event\n"; 20: 21: t1 = new JTextArea( s, 10, 15 ); 22: b.add( new JScrollPane( t1 ) ); 23: 24: copy = new JButton( "Copy >>>" ); 25: copy.addActionListener( 26: new ActionListener() { 27: public void actionPerformed( ActionEvent e ) 28: { 29: t2.setText( t1.getSelectedText() ); 30: } 31: } 32: ); 33: b.add( copy ); 34: 35: t2 = new JTextArea( 10, 15 ); 36: t2.setEditable( false ); 37: b.add( new JScrollPane( t2 ) ); 38: add(b); 39: } 40: } 41: 42: public class TextAreaDemo extends JApplet 43: { 44: TextAreaPanel panel; 45: public void init() 46: { 47: panel = new TextAreaPanel(); 48: getContentPane().add(panel); 49: } 50: 51: public static void main( String args[] ) 52: { 53: TextAreaPanel panel = new TextAreaPanel(); 54: JFrame frame = new JFrame("TextArea Demo"); 55: frame.add(panel); 56: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 57: frame.pack(); 58: //frame.setSize( 425, 200 ); 59: frame.setVisible(true); 60: } 61: }
Maintained by John Loomis, updated Tue Mar 25 12:00:20 2008