Issue
public class FirstStage{
@FXML
private Label HighScore;
@FXML
private TextField Player_Name;
@FXML
private Button closedButton;
@FXML
private Button Button_Play;
@FXML
void ButtonPlay(ActionEvent event) throws Exception{
if(Player_Name.getText().isEmpty()) {
Button_Play.setDisable(true);
Player_Name.setPromptText("Please Enter Your Name");
}
else{
String name = Player_Name.getText();
new SecondStage(name);
}
}
This is my MainController.java. I want the button to be enable after the user have input the text. Does anyone know how to enable back the button after user already fill in the text.
Solution
You can bind
the Button
disable property with the TextField
text property (I modified the names to comply with Java naming conventions):
buttonPlay.disableProperty().bind(playerName.textProperty().isEmpty());
or if you need a minimum number of letters:
int minLength = 3;
buttonPlay.disableProperty().bind(
playerName.textProperty().length().lessThan(minLength));
Note: You should do the binding
in the controller
initializer.
Answered By - Oboe
Answer Checked By - Timothy Miller (JavaFixing Admin)