我发现很难清除JFormattedTextField
s 的值。怎么做?
我试过
txtJFormattedTextField.setText("");
但是当再次失去焦点时,我清除的值会出现。我阅读了有关此问题的 API。
我为日期字段创建 JTFormattedTextFields 的工厂如下 -
public static JFormattedTextField createDateField() {
MaskFormatter maskFormatter = null;
try {
maskFormatter = new MaskFormatter("##/##/####");
} catch (ParseException e) {
e.printStackTrace();
}
JFormattedTextField formattedTextField = new JFormattedTextField(
maskFormatter);
SwingHelper.installDateValidation((AbstractDocument) formattedTextField
.getDocument());
formattedTextField.setColumns(10);
return formattedTextField;
}
试过了
try {
((JFormattedTextField) txtSigninDate).commitEdit();
} catch (ParseException e) {
e.printStackTrace();
}
结果是异常 - 下面是异常跟踪。
java.text.ParseException: stringToValue passed invalid value
at javax.swing.text.MaskFormatter.stringToValue(MaskFormatter.java:456)
at javax.swing.text.MaskFormatter.stringToValue(MaskFormatter.java:371)
at javax.swing.JFormattedTextField.commitEdit(JFormattedTextField.java:530)
at app.view.action.Authentication_Action.clearSigninFields(Authentication_Action.java:207)
at app.view.action.authentication.AuthenticationCommand.decorateSignout(AuthenticationCommand.java:285)
at app.view.action.authentication.AuthenticationCommand.executeSignin(AuthenticationCommand.java:122)
at app.view.action.Authentication_Action$2.actionPerformed(Authentication_Action.java:292)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)