5

I'm working on a sudoku solver and I'm trying to limit the amount of numbers a user can enter into a cell as well as ONLY being able to enter numbers, currently it also accepts letters. At the moment it works like this: If a user enters "11125455876" it will just use the last number entered (in this case "6"). I would like to limit the amount of numbers a user can enter into a single cell to 1 and only accept numbers 1-9. I'm not really sure how I can do that. So maybe it'll just overwrite the previous number or something and if I press a letter key as "a" nothing should happen.

Here is my code:

public PanelGUI(int size) {

    super(size);
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumIntegerDigits(1);
    f.setMinimumIntegerDigits(0);
    f.setGroupingUsed(false);
    cells = new JTextField[size][size];
    panel.setLayout(new GridLayout(size, size));
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            cells[i][j] = new JFormattedTextField(f);
            cells[i][j].setEditable(true);
            cells[i][j].setFont(boldFont);
            cells[i][j].setBorder(BorderFactory.createMatteBorder(1, 1, 1,
                    1, Color.white));
            cells[i][j].setHorizontalAlignment(JTextField.CENTER);
            panel.add(cells[i][j]);
        }
    }
}

As you can see I'm currently using number format.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rob
  • 315
  • 4
  • 15
  • 1
    Use a [`DocumentFilter`](http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/DocumentFilter.html) to limit inputs. – Extreme Coders Apr 10 '13 at 16:15
  • @Extreme Coders Could you write an answer of how it should look like in my case? I'm not too familiar with that. What needs to be changed? – Rob Apr 10 '13 at 16:20
  • http://stackoverflow.com/questions/3519151/how-to-limit-the-number-of-characters-in-jtextfield – herinkc Apr 10 '13 at 16:24
  • See ***[this](http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter)*** link – Extreme Coders Apr 10 '13 at 16:24

2 Answers2

3
NumberFormat f = NumberFormat.getInstance();
f.setMaximumIntegerDigits(1);
f.setMinimumIntegerDigits(0);
f.setGroupingUsed(false);

You want to use a MaskFormat instread:

MaskFormatter f = new MaskFormatter( "#" );
f.setValidCharacters("123456789");
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I tried what you mentioned but it gives me an error and tells me to either add a throw declaration or surround with try/catch. Didn't get it to work with those though. – Rob Apr 10 '13 at 16:35
  • 1
    So add a try/catch. This is standard Java programming and nothing specifically to do with this code. You need to know how to handle exceptions. You can always search the forum/wed for example that use try/catch. – camickr Apr 10 '13 at 18:21
0

You can write your own formatter as said in the below link:

JTextField limiting character amount input and accepting numeric only

Community
  • 1
  • 1
Lavanya
  • 319
  • 1
  • 6