0

I'm doing a Sudoku solver and for that I want my JTextFields to only accept one of the numbers 123456789 as valid input. Therefore I use a MaskFormatter toghether with a JFormattedTextField. However when I clear all the TextFields by doing .setText("") the MaskFormatter doesn't work anymore. After clearing the textboxes I can write anything in them again. Why and how do I fix it?

My code is basically:

MaskFormatter formatter = new MaskFormatter("#");
formatter.setValidCharacters("123456789");
Font textFieldFont = new Font("Verdana", Font.BOLD, 30);
for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
        southPanel.setBorder(lineBorder);
        field[i][j] = new JFormattedTextField(formatter);
        field[i][j].setHorizontalAlignment(JTextField.CENTER);
        field[i][j].setFont(textFieldFont);
        southPanel.add(field[i][j]);
    }
}

Then when I clear it:

for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
        field[i][j].setText("");
    }
}

EDIT: Here is all the code, haven't written most of it cuz my friend did it. I'm just now taking over to fix the GUI a little bit.

http://dl.dropbox.com/u/4018313/SudokuSolver.zip

Also, after some more testing it seems like after clearing all the boxes you can type a lot of charachters that should not be there but when you click on another field all of them will disappear. Then if you click in the other boxes the numbers you wrote earlier will appear.

Don't get this!

Alex
  • 731
  • 1
  • 11
  • 24
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 20 '12 at 17:49
  • 2
    *"one of the numbers 123456789 as valid input."* Use a [`JSpinner`](http://stackoverflow.com/questions/9344708/jcombobox-to-list-age/9345991#9345991). – Andrew Thompson Feb 20 '12 at 17:51
  • A JSpinner is like a drop down menu? EDIT: Now I see what it is but there is so many (9*9 = 81) boxes so that would noot look very good :) – Alex Feb 20 '12 at 17:54
  • *"I see what it is but there is so many (9*9 = 81) boxes so that would noot look very good"* I do not understand. Are you saying the number range is from 1 to 81? – Andrew Thompson Feb 20 '12 at 18:20
  • @Andrew Thompson could you able to findout and tracing question by Jeanette on OTN, because I can't found way how to searching threads older than one year, eeeeeeerrrght – mKorbel Feb 20 '12 at 18:27
  • @mKorbel I'm no expert on OTN search, sorry. Often I will use Google to search the OTN. – Andrew Thompson Feb 20 '12 at 18:35
  • 1
    See also [`CellTest`](http://stackoverflow.com/a/4151403/230513). – trashgod Feb 20 '12 at 21:42

1 Answers1

1

I can't tell you the exact reason but setText seems to drive your JFormattedTextField crazy because "" is a String and it is against the current mask.

Please try using setValue(null) instead.

I've just made sure that this method works. The next piece of code proves it:

public class Two extends JFrame {

    public static void main(String[] args) throws Exception {
        new Two().a();
    }

    void a() throws Exception {
        this.setLayout(new GridLayout(2, 1));
        MaskFormatter formatter = new MaskFormatter("#");
        formatter.setValidCharacters("123456789");
        final JFormattedTextField field = new JFormattedTextField(formatter);
        JButton b = new JButton("null!");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                field.setValue(null);
            }
        });
        this.add(field);
        this.add(b);
        this.setSize(100, 100);
        this.setVisible(true);
    }
}

After clicking the null! button formatter continues to work as it is supposed to work.

Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
  • not easy job setValue(null), can you do that correctly for JFormattedTextField, really this's evil, I'd have to find out nice thread on another forum – mKorbel Feb 20 '12 at 18:13
  • Yeah I tried to add an empty string to the valid charachters list before but I don't think it's possible since it's an empty string. Also tried you suggestion but it was the same! – Alex Feb 20 '12 at 18:18
  • @Qwe I'll update my post in this thread, please can you correct me – mKorbel Feb 20 '12 at 18:21
  • Found this: http://tech.chitgoks.com/2010/06/09/allow-maskformatter-to-accept-empty-value-in-java/ not sure how my teachers are gonna feel about that though :) – Alex Feb 20 '12 at 18:22
  • @mKorbel please try to run the class from my answer - it works for me without any exceptions (Java7) – Oleg Mikheev Feb 20 '12 at 18:46
  • Hmm, you code works for me too. So I tried to set my Reset button to just reset the upper ringht textfield field[0][0] and if I do that it works just as well as your code. So I gues it has something to to with the multiple calls taking to long or what? – Alex Feb 20 '12 at 19:45
  • Ok, so I have now found what the issue was. The Issue was that I used the same MaskFormatter for all of the textfields. I noticed that the first one always worked perfectly but not the others so I created 81 identical MaskFormatters and it works awesome now! It doesn't matter if I use "" or null. – Alex Feb 20 '12 at 21:02