Java Reference
In-Depth Information
1
// Fig. 12.47: TextAreaFrame.java
2
// Copying selected text from one JText area 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
8
import javax.swing.JTextArea;
import javax.swing.JButton;
9
10
11
import javax.swing.JScrollPane;
public class TextAreaFrame extends JFrame
12
{
13
private final JTextArea textArea1; // displays demo string
private final JTextArea textArea2; // highlighted text is copied here
14
15
private final 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 );
box.add( new JScrollPane(textArea1)); // add scrollpane
27
28
29
copyJButton = new JButton( "Copy >>>" ); // create copy button
30
box.add(copyJButton); // add copy button to box
31
copyJButton.addActionListener(
32
new ActionListener() // anonymous inner class
33
{
34
// set text in textArea2 to selected text from textArea1
35
@Override
36
public void actionPerformed(ActionEvent event)
37
{
38
textArea2.setText(textArea1.getSelectedText());
39
}
40
}
41
);
42
43
textArea2 = new JTextArea( 10 , 15 );
textArea2.setEditable( false );
box.add( new JScrollPane(textArea2)); // add scrollpane
44
45
46
47
add(box); // add box to frame
48
}
49
} // end class TextAreaFrame
Fig. 12.47 | Copying selected text from one JTextArea to another.
In the constructor (lines 18-48), line 21 creates a Box container (package
javax.swing ) to organize the GUI components. Box is a subclass of Container that uses
 
Search WWH ::




Custom Search