我在我的studentIDText字段上附加了一个监听器,该字段可从数据库中获取Student记录。但是,如果我将该字段留空,则会显示错误消息。但是,当我单击“退出”按钮时,仍然会收到错误消息,直到再次获取记录或在StudentIdText字段中重新输入内容后,错误消息才会消失。
当单击“退出”按钮时,我试图将焦点设置在另一个字段上(在本例中为隐藏字段)。我还在“姓氏”等其他字段上尝试过,但警报不会消失。
// StudentController.java
public class StudentController {
@FXML private TextField studentIdText;
@FXML private TextField lastNameText;
@FXML private TextField firstNameText;
//Exit the program
@FXML
public void handleExit(ActionEvent actionEvent) {
hiddenText.requestFocus();
System.exit(0);
}
@FXML
public static void showErrorMessage(String infoMessage, String titleBar, String headerMessage)
{
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle(titleBar);
alert.setHeaderText(headerMessage);
alert.setContentText(infoMessage);
alert.showAndWait();
}
@FXML
private void initialize () {
// Listener to studentIdText field
studentIdText.focusedProperty().addListener((observable, wasFocused, focused) -> {
if (!focused && !studentIdText.getText().trim().isEmpty()) {
searchStudent();
}
else {
showErrorMessage("Enter valid Student ID#", "Student DB", "");
studentIdText.requestFocus();
}
});
@FXML
private void searchStudent() throws ClassNotFoundException, SQLException {
if (!studentIdText.getText().isEmpty()) {
//Get Student information
Student stu = StudentDAO.searchStudent(studentIdText.getText());
populateAndShowStudent(stu);
}
}
直到我在StudentIdText字段中输入任何文本之前,错误消息“输入有效的学生ID#”一直显示。还有另一种方法吗?
答案 0 :(得分:0)
如果用户未输入任何内容,则无需搜索学生:
<ul class="urls">
<li>
<label>Toggle</label>
<input class="urlToggler" type="checkbox">
<a class="firstUrl" style="display:none;" href="URL1">Server1 URL1</a>
<a class="secondUrl" style="display:inline;" href="URL2">Server2 URL2</a>
</li>
<li>
<label>Toggle2</label>
<input class="urlToggler" type="checkbox">
<a class="firstUrl" style="display:none;" href="URL1">Server1 URL3</a>
<a class="secondUrl" style="display:inline;" href="URL2">Server2 URL4</a>
</li>
<ul>