使用LostFocus事件在Java中验证Textfield
我正在使用LostFocus事件验证两个文本字段,如下所示:使用LostFocus事件在Java中验证Textfield,java,regex,swing,validation,lostfocus,Java,Regex,Swing,Validation,Lostfocus,我正在使用LostFocus事件验证两个文本字段,如下所示: textRegNo.addFocusListener(new FocusListener() { @Override public void focusLost(FocusEvent arg0) { // TODO Auto-generated method stub regNo1=textRegNo.getText();
textRegNo.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
regNo1=textRegNo.getText();
Pattern pattern1 = Pattern.compile("^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$");
Matcher matcher1 = pattern1.matcher(regNo1);
if (!matcher1.find()){
JOptionPane.showMessageDialog(null, "Invalid Vehicle No!!!\n Vehicle no should be of the form MH 03 KS 2131!!");
}
}
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
};
});
textMobNo.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
mobNo1=textMobNo.getText();
Pattern pattern2 = Pattern.compile("^[789]\\d{9}$");
Matcher matcher2 = pattern2.matcher(mobNo1);
System.out.println("'"+mobNo1+"'");
if (!matcher2.find()){
JOptionPane.showMessageDialog(null, "Phone no must be a 10 digit number!!");
}
}
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
};
});
我的问题是,当我在第一个文本字段上失去焦点并将焦点移动到第二个文本字段时,两个字段都会打印错误消息(两个lostfocus事件下的IF块内的消息)
当我在第一个文本字段中输入错误并将焦点移到第二个字段时,它应该只打印第一个文本字段的错误消息。但这两种方法都存在打印错误
第一个文本字段是textRegNo
第二个文本字段是textMobNo问题的症状围绕着这个过程
textMobNo
获得焦点textRegNo
失去焦点李>
textRegNo
已验证并发现无效,textRegNo
现在显示错误消息textMobNo
失去焦点(因为对话框获得焦点)import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.JTextComponent;
public class TestInputVerifier {
public static void main(String[] args) {
new TestInputVerifier();
}
public TestInputVerifier() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
JTextField field = new JTextField(20);
field.setInputVerifier(new RegExpInputVerifier("^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$"));
add(field, gbc);
field = new JTextField(20);
field.setInputVerifier(new RegExpInputVerifier("^[789]\\d{9}$"));
add(field, gbc);
}
}
public class RegExpInputVerifier extends InputVerifier {
private String expression;
public RegExpInputVerifier(String expression) {
this.expression = expression;
}
public String getExpression() {
return expression;
}
@Override
public boolean verify(JComponent input) {
boolean verified = false;
if (input instanceof JTextComponent) {
JTextComponent field = (JTextComponent) input;
String regNo1 = field.getText();
Pattern pattern1 = Pattern.compile(expression);
Matcher matcher1 = pattern1.matcher(regNo1);
}
return verified;
}
}
}
问题的症状围绕着这个过程
textMobNo
获得焦点textRegNo
失去焦点李>
textRegNo
已验证并发现无效,textRegNo
现在显示错误消息textMobNo
失去焦点(因为对话框获得焦点)import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.JTextComponent;
public class TestInputVerifier {
public static void main(String[] args) {
new TestInputVerifier();
}
public TestInputVerifier() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
JTextField field = new JTextField(20);
field.setInputVerifier(new RegExpInputVerifier("^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$"));
add(field, gbc);
field = new JTextField(20);
field.setInputVerifier(new RegExpInputVerifier("^[789]\\d{9}$"));
add(field, gbc);
}
}
public class RegExpInputVerifier extends InputVerifier {
private String expression;
public RegExpInputVerifier(String expression) {
this.expression = expression;
}
public String getExpression() {
return expression;
}
@Override
public boolean verify(JComponent input) {
boolean verified = false;
if (input instanceof JTextComponent) {
JTextComponent field = (JTextComponent) input;
String regNo1 = field.getText();
Pattern pattern1 = Pattern.compile(expression);
Matcher matcher1 = pattern1.matcher(regNo1);
}
return verified;
}
}
}
尝试使用而不是焦点侦听器来验证字段…问题的症状围绕着这个过程
textMobNo
获得焦点,这会导致textRegNo
失去焦点,textRegNo
被验证并发现无效,textRegNo
现在显示一条错误消息……textMobNo
失去焦点(因为对话框获得焦点)您能为刚才解释的问题提出解决方案吗?谢谢你!是的,使用一个输入验证程序
,这就是他们在那里的目的…好的!我去看看!谢谢你的帮助!尝试使用而不是焦点侦听器来验证字段…问题的症状围绕着这个过程textMobNo
获得焦点,这会导致textRegNo
失去焦点,textRegNo
被验证并发现无效,textRegNo
现在显示一条错误消息……textMobNo
失去焦点(因为对话框获得焦点)您能为刚才解释的问题提出解决方案吗?谢谢你!是的,使用一个输入验证程序
,这就是他们在那里的目的…好的!我去看看!谢谢你的帮助!你能给我一个在我的代码中使用正则表达式的InputVerifier的例子吗。我愿意使用匿名类。我不知道如何在InputVerifier中使用正则表达式。它非常简单。您从focusLost
获取代码,并将其放入InputVerifier
的verify
,以便。。。我们正在使用InputVerifier,但问题是,当您选择另一个窗口时,事件从未触发,因此该字段未及时得到验证。可以采取什么措施来防止这种情况?是否可以通过focusLost事件触发验证?这些事件似乎总是在选择另一个窗口时被触发。您“可以”尝试的唯一一件事是使用FocusdListener强制validation@DeanStrydom您所面临的问题归结为这样一个事实,即窗口焦点的更改只被视为“暂时”焦点丢失,这意味着不会触发输入验证。问题是,为什么要以这种方式验证它?为什么用户不需要返回并继续他们的数据输入,或者选择一些“操作”按钮来“提交”值?你能给我一个在我的代码中使用regex的InputVerifier示例吗。我愿意使用匿名类。我不知道如何在InputVerifier中使用正则表达式。它非常简单。您从focusLost
获取代码,并将其放入InputVerifier
的verify
,以便。。。我们正在使用InputVerifier,但问题是,当您选择另一个窗口时,事件从未触发,因此该字段未及时得到验证。可以采取什么措施来防止这种情况?是否可以通过focusLost事件触发验证?这些事件似乎总是在选择另一个窗口时被触发。您“可以”尝试的唯一一件事是使用FocusdListener强制validation@DeanStrydom您所面临的问题归结为这样一个事实,即窗口焦点的更改只被视为“暂时”焦点丢失,这意味着不会触发输入验证。问题是,为什么要以这种方式验证它?为什么用户不需要返回并继续输入数据,或者选择一些“操作”按钮来“提交”值?