I am doing this assignment, make a program that solves sudoku. I have a panel with a grid of SudokuTextBox extends JFormattedTextField. I have a MaskFormatter so that it onl
Although not the identical question, I was searching through the (too many) questions like this one. In my case I wanted to be able to fill two other fields (jtfStarePatReq and jtfStarePatFound which are JTextFields) either by looking up an index directly or by using some spinners and creating a string and then looking that string up (okay, maybe that is too vague but I think it is enough context). What I wanted is that if the user deleted or cleared out the value in the JFormattedTextField jftfStareOpsIndex, then the other two fields would be cleared as well. I was using the .isEmpty() method on the JFormattedTextField to decide if I should use that field OR use the longer computed search method. So I NEED it to be empty if someone goes from looking up their own index to letting the software search for the index. Anyway, I tried catching the commitEdit() exception and setting the value to null and it seems to do the trick.
public class stareOpsIndexListener extends KeyAdapter implements FocusListener {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
try {
JFormattedTextField jftf = (JFormattedTextField) e.getComponent();
jftf.commitEdit();
updateStareOpsIndex(jftf);
} catch (ParseException ex) {
jtfStarePatReq.setText("");
jtfStarePatFound.setText("");
jftfStareOpsIndex.setValue(null);
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
try {
JFormattedTextField jftf = (JFormattedTextField) e.getComponent();
jftf.commitEdit();
updateStareOpsIndex(jftf);
} catch (ParseException ex) {
jtfStarePatReq.setText("");
jtfStarePatFound.setText("");
jftfStareOpsIndex.setValue(null);
}
}
}
}