2

I am making a little Sudoku game with a GUI and use a MaskFormatter for the JFormattedTextFields:

 formatter = new MaskFormatter(s);
 formatter.setValidCharacters("123456789");

But my problem is that when the window opens and i click into one of those fields, it is not possible to type something,
it only works on second try, namely when I click into another field and then back to the first one.

Is it like it has to lose focus first to activate?

If some of my code is necessary please let me know.

Here's what it looks like

enter image description here

EDIT: The problem was here:

if (guessMatrix[i][j] == 0) {
    tfM[j][i].setBackground(Color.yellow);
    tfM[j][i].setText("");

Without the setText("") it works perfectly fine.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Big_Chair
  • 2,781
  • 3
  • 31
  • 58
  • `If some of my code is necessary please let me know` - Post a [SSCCE](http://sscce.org/) that demonstrates the problem. Chances are while you create the SSCCE you will find the problem. You should not need to invoke requestFocusInWindow() since focus automatically goes to the first component. – camickr May 20 '13 at 19:22
  • @camickr You were right, I just found the problem, I'll edit my post – Big_Chair May 20 '13 at 19:26
  • See also this [alternate](http://stackoverflow.com/a/4151403/230513) approach. – trashgod May 20 '13 at 20:24

1 Answers1

1

I think you have problem in create object of MaskFormatter. you create object with pattern of masking and then after set valid characters.

 MaskFormatter formatter = new MaskFormatter("#");
 formatter.setValidCharacters("123456789");
 JFormattedTextField txt = new JFormattedTextField(formatter);

This work perfectly, when you click on textfield and type any number(1-9 only) it allow but you type any non-number then not allow.

Thanks, Jignesh Gothadiya