/Users/lyon/j4p/src/gui/run/RunTextArea.java
|
1 package gui.run;
2
3 import javax.swing.*;
4 import javax.swing.text.Document;
5 import java.awt.event.ActionListener;
6 import java.awt.event.KeyEvent;
7 import java.awt.event.KeyListener;
8
9 /**
10 * Just like the RunTextFieldOld except that each
11 * char causes an invocation of the run method
12 */
13 public abstract class RunTextArea extends
14 JTextArea
15 implements ActionListener,
16 KeyListener,
17 Runnable {
18 private String originalText = null;
19
20 public void keyTyped(KeyEvent e) {
21 };
22
23 public void keyPressed(KeyEvent e) {
24 };
25
26 public void keyReleased(KeyEvent e) {
27 run();
28 };
29
30 public String getOriginalText() {
31 return originalText;
32 }
33
34
35 public RunTextArea(String text) {
36 super(text);
37 originalText = text;
38 addKeyListener(this);
39
40 }
41
42 /**
43 * Constructs a new empty TextArea with the
44 * specified number of rows and columns. A
45 * default model is created, and the initial
46 * string is null.
47 *
48 * @param rows the number of rows >= 0
49 * @param columns th?e number of columns >= 0
50 * @throws IllegalArgumentException if the rows
51 * or columns
52 * arguments
53 * are negative.
54 */
55 public RunTextArea(int rows, int columns) {
56 super(rows, columns);
57 }
58
59 /**
60 * Constructs a new TextArea with the
61 * specified text and number of rows and
62 * columns. A default model is created.
63 *
64 * @param text the text to be displayed, or
65 * null
66 * @param rows the number of rows >= 0
67 * @param columns the number of columns >= 0
68 * @throws IllegalArgumentException if the rows
69 * or columns
70 * arguments
71 * are negative.
72 */
73 public RunTextArea(String text,
74 int rows,
75 int columns) {
76 super(text, rows, columns);
77 addKeyListener(this);
78 }
79
80 /**
81 * Constructs a new JTextArea with the given
82 * document model, and defaults for all of the
83 * other arguments (null, 0, 0).
84 *
85 * @param doc the model to use
86 */
87 public RunTextArea(Document doc) {
88 super(doc);
89 addKeyListener(this);
90 }
91
92 /**
93 * Constructs a new JTextArea with the
94 * specified number of rows and columns, and
95 * the given model. All of the constructors
96 * feed through this constructor.
97 *
98 * @param doc the model to use, or create
99 * a default one if null
100 * @param text the text to be displayed,
101 * null if none
102 * @param rows the number of rows >= 0
103 * @param columns the number of columns >= 0
104 * @throws IllegalArgumentException if the rows
105 * or columns
106 * arguments
107 * are negative.
108 */
109 public RunTextArea(Document doc,
110 String text,
111 int rows,
112 int columns) {
113 super(doc, text, rows, columns);
114 addKeyListener(this);
115 }
116
117
118 public RunTextArea() {
119 this("");
120 addKeyListener(this);
121 }
122
123 public void actionPerformed(
124 java.awt.event.ActionEvent e) {
125 run();
126 }
127
128 public static void main(String args[]) {
129 gui.ClosableJFrame cf = new gui.ClosableJFrame(
130 "RunTextArea");
131 java.awt.Container c = cf.getContentPane();
132 c.add(new RunTextArea(
133 "What is your name?") {
134 public void run() {
135 System.out.println(getText());
136 }
137 });
138 c.add(new RunTextArea(
139 "What is your name?") {
140 public void run() {
141 System.out.println(getText());
142 }
143 });
144 c.setLayout(
145 new java.awt.GridLayout(4, 0));
146 cf.setSize(200, 200);
147 cf.setVisible(true);
148 }
149
150 }