-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatTestFrame.java
More file actions
151 lines (130 loc) · 4.71 KB
/
FormatTestFrame.java
File metadata and controls
151 lines (130 loc) · 4.71 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package textFormat;
import java.awt.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
/**
* A frame with a collection of formatted text fields and a button that displays the field values.
*/
public class FormatTestFrame extends JFrame
{
private DocumentFilter filter = new IntFilter();
private JButton okButton;
private JPanel mainPanel;
public FormatTestFrame()
{
JPanel buttonPanel = new JPanel();
okButton = new JButton("Ok");
buttonPanel.add(okButton);
add(buttonPanel, BorderLayout.SOUTH);
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0, 3));
add(mainPanel, BorderLayout.CENTER);
JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance());
intField.setValue(new Integer(100));
addRow("Number:", intField);
JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
intField2.setValue(new Integer(100));
intField2.setFocusLostBehavior(JFormattedTextField.COMMIT);
addRow("Number (Commit behavior):", intField2);
JFormattedTextField intField3 = new JFormattedTextField(new InternationalFormatter(
NumberFormat.getIntegerInstance())
{
protected DocumentFilter getDocumentFilter()
{
return filter;
}
});
intField3.setValue(new Integer(100));
addRow("Filtered Number", intField3);
JFormattedTextField intField4 = new JFormattedTextField(NumberFormat.getIntegerInstance());
intField4.setValue(new Integer(100));
intField4.setInputVerifier(new InputVerifier()
{
public boolean verify(JComponent component)
{
JFormattedTextField field = (JFormattedTextField) component;
return field.isEditValid();
}
});
addRow("Verified Number:", intField4);
JFormattedTextField currencyField = new JFormattedTextField(NumberFormat
.getCurrencyInstance());
currencyField.setValue(new Double(10));
addRow("Currency:", currencyField);
JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance());
dateField.setValue(new Date());
addRow("Date (default):", dateField);
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
format.setLenient(false);
JFormattedTextField dateField2 = new JFormattedTextField(format);
dateField2.setValue(new Date());
addRow("Date (short, not lenient):", dateField2);
try
{
DefaultFormatter formatter = new DefaultFormatter();
formatter.setOverwriteMode(false);
JFormattedTextField urlField = new JFormattedTextField(formatter);
urlField.setValue(new URL("http://java.sun.com"));
addRow("URL:", urlField);
}
catch (MalformedURLException ex)
{
ex.printStackTrace();
}
try
{
MaskFormatter formatter = new MaskFormatter("###-##-####");
formatter.setPlaceholderCharacter('0');
JFormattedTextField ssnField = new JFormattedTextField(formatter);
ssnField.setValue("078-05-1120");
addRow("SSN Mask:", ssnField);
}
catch (ParseException ex)
{
ex.printStackTrace();
}
JFormattedTextField ipField = new JFormattedTextField(new IPAddressFormatter());
ipField.setValue(new byte[] { (byte) 130, 65, 86, 66 });
addRow("IP Address:", ipField);
pack();
}
/**
* Adds a row to the main panel.
* @param labelText the label of the field
* @param field the sample field
*/
public void addRow(String labelText, final JFormattedTextField field)
{
mainPanel.add(new JLabel(labelText));
mainPanel.add(field);
final JLabel valueLabel = new JLabel();
mainPanel.add(valueLabel);
okButton.addActionListener(event ->
{
Object value = field.getValue();
Class<?> cl = value.getClass();
String text = null;
if (cl.isArray())
{
if (cl.getComponentType().isPrimitive())
{
try
{
text = Arrays.class.getMethod("toString", cl).invoke(null, value)
.toString();
}
catch (ReflectiveOperationException ex)
{
// ignore reflection exceptions
}
}
else text = Arrays.toString((Object[]) value);
}
else text = value.toString();
valueLabel.setText(text);
});
}
}