Question

So, I finished making a Sudoku solver but I want to improve it. To do this I somehow need to reach my betterJTextField from the documentListener. I'm using a documentListener to read in real-time from my betterJTextFields, the problem I have is that in the insertUpdate(DocumentEvent e).

I need to reach the betterJTextfield that the DocumentEvent happened in. For example, if invalid input, the betterJTextfield will turn red etc.

I have all my betterJTextfield in a matrix if you need to know. Every field handles one number in the Sudoku.

@Override
    public void insertUpdate(DocumentEvent e) {

       //Removed code which checks if the input in the betterJTextField is fine. 

    }

(JFormattedTextfield extends JTextField)

public class betterJTextField extends JFormattedTextField {
private int row;
private int column;

public betterJTextField(Format format, int row, int column) {
    super(format);
    this.row = row;
    this.column = column;
    // TODO Auto-generated constructor stub
}

public int getRow() {
    return row;
}

public int getColumn() {
    return column;
}
Was it helpful?

Solution

I don't really understand completely what you are asking, but I believe this is what you are looking for:

private static class RedDocumentListener implements DocumentListener {
    private JTextField textField;

    public RedDocumentListener(JTextField textField) {
        this.textField = textField;
    }
    @Override
    public void insertUpdate(DocumentEvent e) {
        textField.setBackground(Color.red);
    }
    @Override
    public void removeUpdate(DocumentEvent e) {
        textField.setBackground(Color.red);
    }
    @Override
    public void changedUpdate(DocumentEvent e) {
        textField.setBackground(Color.red);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top