我正在尝试创建一个具有起始日期的简单清单,以响应指定日期的时间滑块。
每个任务都有开始日期。
每个任务复选框仅应在任务开始日期已过时呈现。
清单在初始化时看起来像这样
如何更新清单以响应时间滑块?
我知道我需要在某个地方添加一个侦听器或打电话以更新场景中某个地方的ui元素,但是我不确定从哪里开始。
我尝试更新用于创建的列表,但是由于已经创建了UI元素,所以它没有更新。
ProjectChecklist.java
public class ProjectChecklist extends Application {
Group root = new Group();
Scene scene = new Scene(root, 600, 300);
VBox vbox = new VBox();
HBox hbox = new HBox();
ScrollPane scrollPane = new ScrollPane();
taskList = getAllTasks(PROJECT_FILENAME);
todayDate = LocalDate.now();
sliderStartDate = taskList.get(0).getStartDate();
LocalDate sliderEndDate = taskList.get(taskList.size() - 1).getEndDate();
long days = ChronoUnit.DAYS.between(sliderStartDate, sliderEndDate);
long daysFromToday = ChronoUnit.DAYS.between(sliderStartDate, todayDate);
slider = new Slider(0, days, 1);
slider.setValue(daysFromToday);
sliderValue = new Label(LocalDate.now().toString());
slider.setMajorTickUnit(1);
slider.setShowTickLabels(true);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
slider.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double daysValue = Math.round(newValue.doubleValue());
LocalDate newDate = (sliderStartDate.plusDays((long) daysValue));
slider.setValue(daysValue);
sliderValue.setText(newDate.toString());
}
int taskNumber = 1;
for (ChecklistTask task : taskList) {
if (!task.getStartDate().isAfter(todayDate)) {
CheckBox newCB = new CheckBox();
newCB.setText("task " + taskNumber++ + ": " + task.getDescription());
vbox.getChildren().add(newCB);
}
}
vbox.getChildren().add(slider);
vbox.getChildren().add(sliderValue);
hbox.getChildren().add(vbox);
hbox.setSpacing(40);
hbox.setPadding(new Insets(10, 10, 10, 10));
scrollPane.setContent(hbox);
scrollPane.setPrefSize(600, 300);
scrollPane.setFitToHeight(true);
root.getChildren().add(scrollPane);
primaryStage.setTitle("Bennington checklist");
primaryStage.setScene(scene);
}
public static void main(String args[]) {
launch(args);
runApplication();
}
ChecklistTask.java
import java.time.LocalDate;
import java.util.UUID;
public class ChecklistTask{
private UUID id;
private String description;
private LocalDate startDate;
private LocalDate endDate;
public ChecklistTask() {
this.id = UUID.randomUUID();
}
public UUID getId() {
return id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDate getEndDate() {
return endDate;
}
public void setEndDate(LocalDate endDate) {
this.endDate = endDate;
}
public LocalDate getStartDate() {
return startDate;
}
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}
}
答案 0 :(得分:0)
我建议使用由FilteredList支持的ListView,其中谓词将滑块值与ChequelistTask日期进行比较。更改滑块的值将自动更新谓词,以过滤列表的开始日期>滑块的选定日期。过滤将负责相应地更新列表,而无需重建VBox内容。
下面是一个在某些情况发生变化时更新谓词的示例: JavaFX FilteredList, filtering based on property of items in the list