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 9149329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:27:44+00:00 2026-06-17T11:27:44+00:00

Possible Duplicate: Restricting JTextField input to Integers Detecting JTextField deselect event i need to

  • 0

Possible Duplicate:
Restricting JTextField input to Integers
Detecting JTextField “deselect” event

i need to validate a JTextField by allowing the user to input only integer values in it if user enters any char other than numbers a JOptionPane.show messagebox should appear showing that the value entered are incorrect and only integer numbers are allowed. I have coded it for a digit values but i also need to discard the alphabets

public void keyPressed(KeyEvent EVT) {
    String value = text.getText();
    int l = value.length();
    if (EVT.getKeyChar() >= '0' && EVT.getKeyChar() <= '9') {
        text.setEditable(true);
        label.setText("");
    } else {
        text.setEditable(false);
        label.setText("* Enter only numeric digits(0-9)");
    }
}
  • 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-17T11:27:45+00:00Added an answer on June 17, 2026 at 11:27 am

    Instead of using a JFormattedTextField, you may write a custom JTextField with a document that allows only integers. I like formatted fields only for more complex masks…
    Take a look.

    import javax.swing.JTextField;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.PlainDocument;
    
    /**
     * A JTextField that accepts only integers.
     *
     * @author David Buzatto
     */
    public class IntegerField extends JTextField {
    
        public IntegerField() {
            super();
        }
    
        public IntegerField( int cols ) {
            super( cols );
        }
    
        @Override
        protected Document createDefaultModel() {
            return new UpperCaseDocument();
        }
    
        static class UpperCaseDocument extends PlainDocument {
    
            @Override
            public void insertString( int offs, String str, AttributeSet a )
                    throws BadLocationException {
    
                if ( str == null ) {
                    return;
                }
    
                char[] chars = str.toCharArray();
                boolean ok = true;
    
                for ( int i = 0; i < chars.length; i++ ) {
    
                    try {
                        Integer.parseInt( String.valueOf( chars[i] ) );
                    } catch ( NumberFormatException exc ) {
                        ok = false;
                        break;
                    }
    
    
                }
    
                if ( ok )
                    super.insertString( offs, new String( chars ), a );
    
            }
        }
    
    }
    

    If you are using NetBeans to build your GUI, you just need to put regular JTextFields in your GUI and in the creation code, you will specify the constructor of IntegerField.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Restricting WCF Service access to only localhost I have a WCF method
Possible Duplicate: JQuery datepicker- 2 inputs/textboxes and restricting range Hi i have two pickers
Possible Duplicate: How would one implement a NumberPicker in Android API 7? I need
Possible Duplicate: Using jQuery, Restricting File Size Before Uploading Is there a good jquery
Possible Duplicate: Get the size of a list in python? I need to be
Possible Duplicate: Matching an integer between the brackets What is the regex for matching
Possible Duplicate: Do you need Mac os x to develop IPhone apps? I am
Possible Duplicate: Objective-C dictionary inserting a BOOL I need to add key-value pair NSIndexPath
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web

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.