Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8898749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:41:25+00:00 2026-06-15T00:41:25+00:00

I have 2 textfields in a JFrame and I want to validate the data

  • 0

I have 2 textfields in a JFrame and I want to validate the data in textfield1 when the focus gets lost from textfield1. So I have used FocusListener and used showMessageDialog() in the FocusLost() method and that then sets back the focus back to textfield1. It works fine when I click on any component inside the JFrame window other than textfield1,but when I click anywhere outside the JFrame window, the showMessageDialog() gets called two times and the focus goes to textfield2 whereas the focus should remain on textfield1.

    @Override
    public void focusGained(FocusEvent e) {}

    @Override
    public void focusLost(FocusEvent e) {
        boolean show = false;
        String theRegex = "[0-9]";
        Pattern checkRegex = Pattern.compile(theRegex);
        Matcher regexMatcher = checkRegex.matcher( MemberID );
        while ( !regexMatcher.find() && show==false){
            JOptionPane.showMessageDialog(null,"Please enter numbers","Validation Error",JOptionPane.ERROR_MESSAGE);
            MemberID_Text.requestFocusInWindow();
    MemberID_Text.selectAll();
            show = true;

        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:41:27+00:00Added an answer on June 15, 2026 at 12:41 am

    you can do this to verify if a number is entered, and avoid regex all together

         class IntVerifier extends InputVerifier {
    
      @Override public boolean verify(JComponent input) {
          String text =((JTextField) input).getText();
          int n = 0; 
    
              try {
          n = Integer.parseInt(text); } 
    
          catch (NumberFormatException e) {
      return false; 
           }
    
      return true;
          }
          }
    

    then use the input verifier on the text field

     IntVerifier intv = new IntVerifier();      
     myTextField = new JTextField();
     myTextField.setInputVerifier(intv);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have 3 textfields on a JFrame Form. I need to get
In a Swing project, I have used a JFrame and divided it into three
I have a JFrame that I want closed when the user clicks off of
I have a JFrame panel. It opens several times. However, I want the previous
i have a JTextField inside a JFrame, and I want it to find the
I have two JFormattedTextField objects on my JFrame object. I want a basic Math
I have a JFrame and In which I want to show a JInternal Frame
I have 8 textfields in one view. I want to populate each using a
I have a JFrame with about 10 components (JLabel, Combobox, Buttons, TextFields). When I
My text Field is not being added to my Jframe, I want to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.