JAVA; Accept blank field in Text Field Integer Restriction

38 Views Asked by At

I have a problem here. I want to restrict my Text Field into a positive integers only. It works because I copied it from another Stack forum but the problem is that I can't remove the first character once I already inputted a number.

For example: I inputted 123 but I changed my mind so I will backspace/erase all. They are not all erased, the number 1 is still there. Please help me, how to except ""/blank field in the number format.

        NumberFormat format = NumberFormat.getInstance();
        NumberFormatter formatter = new NumberFormatter(format);
        formatter.setValueClass(Integer.class);
        formatter.setMinimum(0);
        formatter.setMaximum(Integer.MAX_VALUE);
        formatter.setAllowsInvalid(false);
        JFormattedTextField numInput = new JFormattedTextField(formatter);
        numInput.setHorizontalAlignment(SwingConstants.CENTER);
        numInput.setFont(new Font("Arial", Font.PLAIN, 18));
        numInput.setBounds(182, 126, 61, 25);
        frmFibonacciSequence.getContentPane().add(numInput);
        numInput.setColumns(10);
0

There are 0 best solutions below