-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFieldTest.java
More file actions
89 lines (67 loc) · 2.83 KB
/
TextFieldTest.java
File metadata and controls
89 lines (67 loc) · 2.83 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
package test_field_test;
//TextFieldTest.java
//Demonstrating the JTextField class.
//Java core packages
import java.awt.*;
import java.awt.event.*;
//Java extension packages
import javax.swing.*;
public class TextFieldTest extends JFrame {
private JTextField textField1, textField2, textField3;
private JPasswordField passwordField;
// set up GUI
public TextFieldTest() {
super("Testing JTextField and JPasswordField");
Container container = getContentPane();
container.setLayout(new FlowLayout());
// construct textfield with default sizing
textField1 = new JTextField(10);
container.add(textField1);
// construct textfield with default text
textField2 = new JTextField("Enter text here");
container.add(textField2);
// construct textfield with default text and
// 20 visible elements and no event handler
textField3 = new JTextField("Uneditable text field", 20);
textField3.setEditable(false);
container.add(textField3);
// construct textfield with default text
passwordField = new JPasswordField("Hidden text");
container.add(passwordField);
// register event handlers
TextFieldHandler handler = new TextFieldHandler();
textField1.addActionListener(handler);
textField2.addActionListener(handler);
textField3.addActionListener(handler);
passwordField.addActionListener(handler);
setSize(325, 100);
setVisible(true);
}
// execute application
public static void main(String args[]) {
TextFieldTest application = new TextFieldTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// private inner class for event handling
private class TextFieldHandler implements ActionListener {
// process text field events
public void actionPerformed(ActionEvent event) {
String string = "";
// user pressed Enter in JTextField textField1
if (event.getSource() == textField1)
string = "textField1: " + event.getActionCommand();
// user pressed Enter in JTextField textField2
else if (event.getSource() == textField2)
string = "textField2: " + event.getActionCommand();
// user pressed Enter in JTextField textField3
else if (event.getSource() == textField3)
string = "textField3: " + event.getActionCommand();
// user pressed Enter in JTextField passwordField
else if (event.getSource() == passwordField) {
JPasswordField pwd = (JPasswordField) event.getSource();
string = "passwordField: " + new String(passwordField.getPassword());
}
JOptionPane.showMessageDialog(null, string);
}
} // end private inner class TextFieldHandler
} // end class TextFieldTest