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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:10:32+00:00 2026-05-16T16:10:32+00:00

I need a JTextField that only accepts positive integers as input. My implementation mostly

  • 0

I need a JTextField that only accepts positive integers as input. My implementation mostly works. As you type text into the field, the digits appear and non-digits do not appear.

But there is a problem with the constructor’s “text” argument. If I pass a String that contains the String representation of a positive integer, I would expect that the text field would start out containing that field, but instead the text field starts out blank. For example, doing this causes it to exhibit the symptoms I’ve described: new NumericTextField(“1”, 5);

The over-ridden insertString method is not called during initialization.

What am I doing wrong, and how do I fix it? Here’s the code…

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;


/**
  * A JTextField that only accepts characters that are digits. Also, the first character can't be "0".
 */
public class NumericTextField extends JTextField {

    public class CustomFilterDocument extends PlainDocument {
        public void insertString(int offset, String text, AttributeSet aset) throws BadLocationException {
            if (isLegalValue(text)) {
                super.insertString(offset, text, aset);
            }
        }
    }

    private static boolean isLegalValue(String text) {
        if (text == null) {
            return false;
        }

        int len = text.length();
        for (int i = 0; i < len; i++) {
            char c = text.charAt(i);
            if (!Character.isDigit(c) || (i ==0 && c == '0')) {
                return false;
            }
        }

        return true;
    }

    public NumericTextField() {
        super();
        setDocument(new CustomFilterDocument());
    }

    public NumericTextField(int columns) {
        super(columns);
        setDocument(new CustomFilterDocument());
    }

    public NumericTextField(String text) {
        super(text);
        setDocument(new CustomFilterDocument());
    }

    public NumericTextField(String text, int columns) {
        super(text, columns);
        setDocument(new CustomFilterDocument());
    }
}

The fixed version (using what I learned from the responses) looks like this:

public NumericTextField(String text) {
    super(text);
    initText(text);
}

public NumericTextField(String text, int columns) {
    super(text, columns);
    initText(text);
}

private void initText(String text) {
    Document doc = new CustomFilterDocument();
    try {
        doc.insertString(0, text, null);
    } catch (BadLocationException ble) {
    }
    setDocument(doc);
}
  • 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-05-16T16:10:33+00:00Added an answer on May 16, 2026 at 4:10 pm

    The problem is that you are setting the document to be a new document. This will erase any content that you pass in through the constructor.

    public NumericTextField(String text) {
        super(text);
        // This next line will erase the content on the current document
        setDocument(new CustomFilterDocument());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a JTextField (or any component where I can type something)
I need to create one textfield custom components which accepts only credit card number
I have a textfield, in which I need the user to type only numeric
i need to validate a textfield input that allows two digits after a .
i place 4 textfields in my application. I need that 4 textfields allows only
I have two textfield in my swing component. In one text field i need
A label tag is of the form: <label for=id_of_text_field> <input type=text name=example id=id_of_text_field />
I need to validate something about several Wicket input fields of type TextField<BigDecimal> (namely
I am using both JLabel s and JTextField s, and need to be able
I need to display a character symbol (Not alphanumeric, or on the keyword) textField.text

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.