Reputation: 477
I have a choicebox and textfield next to that choicebox in a JavaFX application. I want grey textbox in the textfield to tell the user what to input. However, I want the prompt text to change according to what is selected in the choicebox.
I looked online and found code on how to have a textfield with prompt text but I couldn't get the prompt text to change with a changeListener on the choicebox.
I tried
textfield = new Textfield(newPrompt);
with the textfield already previously declared with a different prompt text. This did not work. How do I achieve the effect of having changing prompt text based on the users selection in the choicebox?
Upvotes: 0
Views: 731
Reputation: 58
Instead of reassigning the textfield
variable to a new TextField
object (via textfield = new TextField(newPrompt);
), use the TextField's setPromptText(String s)
method in your ChangeListener:
final ChoiceBox<String> box = ...; //choicebox created and filled
box.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
textfield.setPromptText("New Prompt Text!");
}
});
Upvotes: 2