How can I achieve a JFormattextfield accepting only number and limited character

Nadim :

I have two JFormattedTextField for variables Amount and Account No .
Purpose :

  1. Both field must accept only number
  2. Acc No can take up to 15 characters which can vary from 8-15. Similarly Amount can have up to 6 characters and also varies.

To achieve this I used MaskFormatter but the problem is the "Variation". Some acc is 15 digits, some are 12 digits so while using MaskFormatter limited to 15, it becomes mandatory to enter 15 digits otherwise inserted data disappears during runtime when we leave the JFormattedTextField

Is there any way to achieve both scenario in java swing?
Please suggest me

camickr :

Use a DocumentFilter. Then you can customize the filter for your specific requirement.

A basic example to get you started:

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

public class DigitFilter extends DocumentFilter
{
    @Override
    public void insertString(FilterBypass fb, int offset, String text, AttributeSet attributes)
        throws BadLocationException
    {
        replace(fb, offset, 0, text, attributes);
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attributes)
        throws BadLocationException
    {
        Document doc = fb.getDocument();

        // add check here check the length of the text currently in the document
        // with the length of your text String to make sure the total is not above the maximum
        // you should modify the class to pass a paramenter when you create the class for the
        // maximum length so the class can be reused

        if (isDigit( text ))
            super.replace(fb, offset, length, text, attributes);
        else
            Toolkit.getDefaultToolkit().beep();
    }

    private boolean isDigit(String text)
    {
        for (int i = 0; i < text.length(); i++)
        {
            if (! Character.isDigit( text.charAt(i) ) )
                return false;
        }

        return true;
    }

    private static void createAndShowGUI()
    {
        JTextField textField = new JTextField(15);
        AbstractDocument doc = (AbstractDocument) textField.getDocument();
        doc.setDocumentFilter( new DigitFilter() );

        JFrame frame = new JFrame("Integer Filter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout( new java.awt.GridBagLayout() );
        frame.add( textField );
        frame.setSize(220, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }

}

You will need to add separate logic (external to the DocumentFilter) to make sure the account length is at least 8 digits before you do your processing.

Read the section from the Swing tutorial on Implementing a Document Filter for an example of a filter that limits the number of characters. The logic from there needs to be combined with the example here.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=317901&siteId=1