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

  • Home
  • SEARCH
  • 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 6155499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:26:45+00:00 2026-05-23T20:26:45+00:00

I need a JTextField limiting to 3 decimal places. After a search, i discovered

  • 0

I need a JTextField limiting to 3 decimal places. After a search, i discovered JFormattedTextField, which looks great. The code right now is:

try
{
    double aux = 25.4;
    MaskFormatter mascara = new MaskFormatter("####.###");
    final JFormattedTextField areaTextoXMin = new   JFormattedTextField(mascara);
    areaTextoXMin.setValue(aux);
}
    catch (Exception e)
{
    e.printStackTrace();
}

But if aux is not 4 integer digits and 3 decimal digits, it doesn’t works, and the JFormattedTextField appears empty. Code would work is aux were 1000.000 for example.

Other ideas?

  • 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-23T20:26:49+00:00Added an answer on May 23, 2026 at 8:26 pm
    import javax.swing.*;
    import javax.swing.text.*;
    import java.util.regex.*;
    public class MyFilter extends DocumentFilter
    {
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
        {
            StringBuilder sb = new StringBuilder();
            sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
            sb.insert(offset, text);
            if(!containsOnlyNumbers(sb.toString())) return;
            fb.insertString(offset, text, attr);
        }
        public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
        {
            StringBuilder sb = new StringBuilder();
            sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
            sb.replace(offset, offset + length, text);
            if(!containsOnlyNumbers(sb.toString())) return;
            fb.replace(offset, length, text, attr);
        }
        public boolean containsOnlyNumbers(String text)
        {
            Pattern pattern = Pattern.compile("\\d*(\\.\\d{0,3})?");
            Matcher matcher = pattern.matcher(text);
            boolean isMatch = matcher.matches();
            return isMatch;
        }
    }
    

    and then you can use it like:

    JTextField txt = new TextField();
    DocumentFilter onlyNumberFilter = new MyFilter();
    ((AbstractDocument)txt.getDocument()).setDocumentFilter(onlyNumberFilter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using both JLabel s and JTextField s, and need to be able
I need a textfield which has an dropdown list to select an option from
I have a starttime as textfield. I need to covert the String which I
I need to change the direction of the growth of JTextField object when more
I have a JTextField and need to have 3 lines in it, such as
I need to do this: Value Change Listener to JTextField I'm trying the solution
I am Doing a Check on the JTextfield where i need input as RD-AK+++++++
In AWT application I need to set border color of TextField. In JTextField, I
I need a JTextField that only accepts positive integers as input. My implementation mostly
I have an application that uses disabled JTextFields in several places which are intended

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.