Textfield Validation in Java using LostFocus event

随声附和 提交于 2019-12-04 22:01:29

The symptom of the problem revolves around this process.

  1. textMobNo gains focus...
  2. which causes textRegNo to lose focus...
  3. textRegNo is validated and found to be invalid, textRegNo now displays an error message...
  4. Focus is transferred to the dialog and...
  5. textMobNo loses focus (because the dialog gained focus)

You should avoid using focus as a means to validate fields if you can and instead use an InputVerifier

Simple example

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;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!