【问题标题】:JavaFX - Alert when text areas are not filledJavaFX - 未填充文本区域时发出警报
【发布时间】:2015-10-24 09:04:22
【问题描述】:

我的问题很小,找不到解决方案。 我创建了一些输入文本区域。他们正在将输入转换为字符串并将它们作为 .txt 文件保存在目录中。 我想创建当用户单击提交按钮时将调用的方法,并检查是否填写了所有请求的字段以及是否应显示错误警报。

            checkRequestedFields();
            String dbSubjectField = inputSubject_field.getText();
            String dbLocationField = "Location: " + inputLocation_field.getText() + "\n";
            String dbDescriptionArea = "Description: " + inputDescription_area.getText() + "\n";
            String dbDate = "Date: " + inputCalendar_datePicker.getValue().toString() + "\n";
            String dbCheckBoxImportance = isSelected() + "\n";
            try
            {
                FileWriter save = new FileWriter("Reminders/" + inputSubject_field.getText() + ".txt");
                BufferedWriter bf = new BufferedWriter(save);
                bf.write(dbDate + dbLocationField + dbDescriptionArea + dbCheckBoxImportance);
                bf.close();
            }
            catch (IOException ex)
            {

            }
            inputDescription_area.clear();
            inputSubject_field.clear();
            inputLocation_field.clear();
            inputCalendar_datePicker.setValue(null);
            inputImportance_checkBox.setSelected(false);
        }

        public void checkRequestedFields()
        {
          if(inputSubject_field.getText() == null || inputLocation_field.getText() == null || inputCalendar_datePicker.getValue().toString() == null || inputDescription_area.getText() == null)
            {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                String content = "Please fill all requested fields";
                alert.setContentText(content);
                alert.showAndWait();
            }
            else
            {

            }
        }
    });

不知道我的方法是否正确,但对我来说很有意义。 如果选定的输入文本区域之一为空(null),则应出现警报。但是当我单击提交按钮时没有任何反应。只是控制台出现了很多错误。

【问题讨论】:

  • 你能发布错误吗?
  • 感谢您的关注。 James_D 的回答有帮助!

标签: java javafx alerts


【解决方案1】:

“空”与null 不同。使用

inputSubject_field.getText().isEmpty()

或许

inputSubject_field.getText().trim().isEmpty()

等等。

【讨论】:

  • 谢谢!修复了我的问题。你能解释一下为什么不一样吗?
  • 一个是null,即没有对象,一个是String,没有字符。
猜你喜欢
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2012-10-20
相关资源
最近更新 更多