-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTextFields.java
More file actions
122 lines (94 loc) · 2.89 KB
/
TextFields.java
File metadata and controls
122 lines (94 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package gui;
/**
* RUN:
* javac -cp .; gui/TextFields.java && java -cp .; gui.TextFields
* OUTPUT:
*
*/
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import net.mindview.util.*;
public class TextFields extends JFrame {
private static final int WIDTH = 375;
private static final int HEIHGT = 200;
private JButton b1 = new JButton("Get Text");
private JButton b2 = new JButton("Set Text");
private JTextField t1 = new JTextField(30);
private JTextField t2 = new JTextField(30);
private JTextField t3 = new JTextField(30);
private String s = "";
private UpperCaseDocument ucd = new UpperCaseDocument();
public TextFields() {
t1.setDocument(ucd);
ucd.addDocumentListener(new T1());
b1.addActionListener(new B1());
b2.addActionListener(new B2());
t1.addActionListener(new T1A());
setLayout(new FlowLayout());
add(b1);
add(b2);
add(t1);
add(t2);
add(t3);
}
class T1 implements DocumentListener {
public void changedUpdate(DocumentEvent e) {}
public void insertUpdate(DocumentEvent e) {
// t2.setText(t1.getText());
t2.setText(t2.getText()+ucd.getOriginalText());
t3.setText("Text: " + t1.getText());
}
public void removeUpdate(DocumentEvent e) {
t2.setText(t1.getText());
}
}
class T1A implements ActionListener {
private int count = 0;
public void actionPerformed(ActionEvent e) {
t3.setText("t1 Action Event: " + count++);
}
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (t1.getSelectedText() == null) {
s = t1.getText();
}
else {
s = t1.getSelectedText();
}
t1.setEditable(true);
}
}
class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
ucd.setUpperCase(false);
t1.setText("Inserted by Button 2: " + s);
ucd.setUpperCase(true);
t1.setEditable(false);
}
}
public static void main(String[] args) {
SwingConsole.run(new TextFields(), WIDTH, HEIHGT);
}
}
class UpperCaseDocument extends PlainDocument {
private boolean upperCase = true;
private String originalText;
public void setUpperCase(boolean flag) {
upperCase = flag;
}
public String getOriginalText() {
return originalText;
}
public void insertString(int offset, String str, AttributeSet attSet)
throws BadLocationException {
originalText = str;
if (upperCase) {
str = str.toUpperCase();
}
super.insertString(offset, str, attSet);
}
}