This is my code but it doesn't work,why?I use the RegexFormatter("@.*") but it seems doesn't work.it can't check the JFormattedTextField whether @.* contain or not.And I want to show the result at the verify label how should I do?
public class hw5 extends JFrame {
private JPanel contentPane;
private JPasswordField passwordField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
hw5 frame = new hw5();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public hw5() {
setTitle("hw5");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFormattedTextField FormattedField = new JFormattedTextField(new RegexFormatter("*@*.*"));
}
}
class RegexFormatter extends DefaultFormatter {
private Pattern pattern;
private Matcher matcher;
public RegexFormatter() {
super();
}
public Object stringToValue(String text) throws ParseException {
Pattern pattern = getPattern();
if (pattern != null) {
Matcher matcher = pattern.matcher(text);
if (matcher.matches()) {
setMatcher(matcher);
return super.stringToValue(text);
}
throw new ParseException("Pattern did not match", 0);
}
return text;
} }