为什么JavaFX日期选择事件在第二次选择日期时有效?

时间:2016-08-03 13:48:03

标签: events javafx datepicker

我编写了一个代码,用于在JavaFX中从DatePicker中选择日期时运行方法。但我的事件代码在第二次选择日期时运行,我不明白为什么会这样。 我的代码是这样的:

public void handleDateSelectEvent(){
    appointmentDay.setOnAction(event -> {
        System.out.println("Tarih Seçildi:"+appointmentDay.getValue());
        java.util.Date selectedDate= java.sql.Date.valueOf(appointmentDay.getValue());
        List<String> avaliableClocks = new ArrayList<String>();
        for (AppointmentClocks ac : findAppointmentClocks(selectedDate)){
            avaliableClocks.add(ac.getClock());
        }

        ObservableList obClocks = FXCollections.observableList(avaliableClocks);
        appointmentClock.getItems().clear();
        appointmentClock.setItems(obClocks);
    });
}

1 个答案:

答案 0 :(得分:1)

您可能正在使用handleDateSelectEvent()作为fxml文件中onAction事件的处理程序。处理程序应直接包含处理代码,而不是注册事件处理程序:

public void handleDateSelectEvent(){
    System.out.println("Tarih Seçildi:"+appointmentDay.getValue());
    java.util.Date selectedDate= java.sql.Date.valueOf(appointmentDay.getValue());
    List<String> avaliableClocks = new ArrayList<String>();
    for (AppointmentClocks ac : findAppointmentClocks(selectedDate)){
        avaliableClocks.add(ac.getClock());
    }

    ObservableList obClocks = FXCollections.observableList(avaliableClocks);
    appointmentClock.getItems().clear();
    appointmentClock.setItems(obClocks);
}