0

I have a JTF8 DocumentFilter class to make Text accept only Numbers (with (-) numbers also), but there's an error on it because it also allows me to type (000) or (00). I need to customize it to deny typing (00) or (000), and allow it to type only one zero in the beginning like this (0) and deny it to make numbers like this (02451).

So, when the user types (0241) or (00241), it will automatically change to (241) and deny them to type (0 with numbers) at the beginning.

They can type (0) but not (00) or (0345)!

My class is:

private static class JTF8DocumentFilter extends DocumentFilter {

    int maxValue;
    int minValue;
    String text;

    private JTF8DocumentFilter(int maxValue, int... optionalMinValue) {
        this.maxValue = maxValue;
        this.minValue = (maxValue - (maxValue * 2));
        this.text = "";

        if (optionalMinValue.length > 0
                && optionalMinValue[0] >= Integer.MIN_VALUE
                && optionalMinValue[0] < maxValue) {
            this.minValue = optionalMinValue[0];
        }
    }

    @Override
    public void replace(DocumentFilter.FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
        text += string;
        int stringValue = 0;
        if (!text.isEmpty() && text.matches("-?(\\d+)?")) {
            if (text.length() == 1 && text.equals("-")) {
                this.text = "-";
                super.insertString(fb, 0, text, as);
                return;
            }
            try {
                stringValue = Integer.parseInt(text);
            } catch (NumberFormatException ex) {
                super.remove(fb, 0, fb.getDocument().getLength());
                this.text = "";
                //          Toolkit.getDefaultToolkit().beep();
                return;
            }
        } else {
            super.remove(fb, 0, fb.getDocument().getLength());
            this.text = "";
            //        Toolkit.getDefaultToolkit().beep();
            return;
        }
        if (string.matches("\\-|\\d") && (stringValue <= maxValue && stringValue >= minValue)) {
            super.replace(fb, i, i1, string, as);
        } else {
            //System.out.println(stringValue);
            if (stringValue > maxValue || stringValue < minValue) {
                super.remove(fb, 0, fb.getDocument().getLength());
                this.text = "";
            }
            //      Toolkit.getDefaultToolkit().beep();
        }
    }

    @Override
    public void remove(DocumentFilter.FilterBypass fb, int i, int i1) throws BadLocationException {
        super.remove(fb, i, i1);
        this.text = fb.getDocument().getText(0, fb.getDocument().getLength());
    }

    @Override
    public void insertString(DocumentFilter.FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
        super.insertString(fb, i, string, as);
    }

}

Note: The answer here is not helping me, it still allows to type (000) not (0) one zero only: DocumentFilter for negative and positive integer

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Adeeb Mark
  • 131
  • 1
  • 8
  • Well, what I want to do is really similar to what you suggested, but it is different from what I wrote. Can you modify the class that i listed in the question ? – Adeeb Mark Jan 21 '21 at 11:47
  • Does this answer your question? [DocumentFilter for negative and positive integer](https://stackoverflow.com/questions/26520487/documentfilter-for-negative-and-positive-integer) – Abra Jan 22 '21 at 05:16
  • no ! the demo class do the same Problem for me – Adeeb Mark Jan 22 '21 at 11:22
  • try it by yourself ! type (00) it will accept it ! And this is what I do not want – Adeeb Mark Jan 22 '21 at 13:41

0 Answers0