2

I want my jtextfield to accept only numerical values between 0 and the MAX value specified in the code example below.

Let´s say the MAX variable is set to 8, as below. Then I just want it to be possible to enter a numeric value between 0 and 8. In my code example below you can type 88, 77, 66 etc and that should not be possible. I have no clue how I can make it so it only accepts values between 0 and MAX.

import javax.swing.*;
import javax.swing.text.*;

public class DocumentDemo extends JFrame {
    public static void main(String[] args) {
        new DocumentDemo();
    }

    final int MAX = 8;

    public DocumentDemo() {
        this.setVisible(true);
        JTextField textField = new JTextField();

        ((AbstractDocument)textField.getDocument()).setDocumentFilter(
                new DocumentFilter() {

                    @Override
                    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                        int len = text.length();
                        boolean isValidValue = true;
                        for(int i = 0; i < len; i++) {
                            if(!Character.isDigit(text.charAt(i))){
                                isValidValue = false;
                            }
                        }

                        if(isValidValue && Integer.parseInt(text) > MAX) {
                            isValidValue = false;
                        }

                        if(isValidValue && fb.getDocument().getText(0, fb.getDocument().getLength()).length() > String.valueOf(MAX).length()) {
                            isValidValue = false;
                        }

                        if(isValidValue) {
                            super.replace(fb, offset, length, text, attrs);
                        }
                    }
                }
        );
        textField.setColumns(5);
        this.add(textField);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rox
  • 2,647
  • 15
  • 50
  • 85
  • 2
    You might want to use a JSpinner, since you have so few valid values. – Gilbert Le Blanc Jun 23 '14 at 21:47
  • The value above is an example. It can be 12345 as well. But I chose a low value in my example above. :-) – Rox Jun 23 '14 at 21:49
  • `"I have no clue how I can make it so it only accepts values between 0 and MAX."` -- A document filter **will** work. Your statement above doesn't help us figure out where you're stuck. Please be specific in your statement of problem. If you search this site on my name and DocumentFilter, you will find examples of how to create these guys. – Hovercraft Full Of Eels Jun 23 '14 at 22:01
  • For example [here](http://stackoverflow.com/a/11093360/522444). Just write an appropriate `test(...)` method that fulfills your criteria. I'm sure you can do this if you try. – Hovercraft Full Of Eels Jun 23 '14 at 22:03

2 Answers2

5

JFormattedTextField sounds like what you want.

NumberFormatter nf = new NumberFormatter();  
nf.setMinimum(new Integer(0));  
nf.setMaximum(new Integer(8));  
JFormattedTextField field = new JFormattedTextField(nf);  

Edit: showed how to set min, max

sevensevens
  • 1,703
  • 16
  • 27
  • Thanks for your reply. But, what I want is to prohibit the user to enter a value bigger than the `MAX` value. – Rox Jun 23 '14 at 21:52
3

You could also use a JSpinner or a DocumentFilter (with examples) depending on your overall needs

A DocumentFilter will provide you with the added benefit of been able to filter the content coming into the field in real time

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366