So I am writing a javafx program to manipulate the individual bits in a byte. I have a textfield for each bit. I want to implement a changelistener on the textfields so one cannot enter anything but a 0 or a 1. It works fine if the field is empty and the user tries to enter a letter, but if there is already a 0 or 1 in it it throws an exception and I dont understand why.
Here is my code:
public class Task03Controller implements Initializable {
@FXML private TextField zeroTextField, oneTextField, twoTextField, threeTextField,
fourTextField, fiveTextField, sixTextField, sevenTextField;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
zeroTextField.textProperty().addListener((observable, oldValue, newValue) -> {
if(!zeroTextField.getText().equals("0") && !zeroTextField.getText().equals("1"))
zeroTextField.clear();
else if(zeroTextField.getText().length() > 1)
zeroTextField.setText(zeroTextField.getText().substring(0, 0));
});
}
}