Steven Vascellaro Steven Vascellaro - 4 years ago 102
Java Question

How do I clear the input field of a TextInputDialog?

My JavaFX program has a series of prompts asking the user for information. Rather than create a new TextInputDialog instance for each prompt, I decided to create a single instance and reuse it for multiple prompts.

Optional<String> textResult;
String name;
double price;
int quantity;

// Fetch user input
try
{
// initialize textDialog
TextInputDialog textDialog = new TextInputDialog();
textDialog.setTitle("Create new item");
textDialog.setHeaderText(null);

textDialog.setContentText("Enter item name:");
textResult = textDialog.showAndWait();
name = textResult.get();

textDialog.setContentText("Enter item price:");
textResult = textDialog.showAndWait();
price = Double.parseDouble( textResult.get() );

textDialog.setContentText("Enter item quantity:");
textResult = textDialog.showAndWait();
quantity = Integer.parseInt( textResult.get() );
}
catch (NumberFormatException error)
{
Alert errorAlert = new Alert( AlertType.ERROR, "Unable to parse field.");
errorAlert.setHeaderText(null);
errorAlert.showAndWait();
}


The only issue is that the user's input from the first prompt isn't cleared when starting the second prompt. Is it possible to clear the textfield between prompts?

Answer Source
textDialog.getEditor().clear();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download