选择一个特定的价值从一个名单Java

0

的问题

我试图建立一个调度程序,在那里我可以更新客户预约时间。 我能救我的约会但是,更新他们已经有点令人困惑。

我有2列出小时和分钟,我正在组合框如下所示。

ObservableList hoursList = FXCollections.observableArrayList();
    hoursList.add("08");
    hoursList.add("09");
    hoursList.add("10");
    hoursList.add("11");
    hoursList.add("12");
    hoursList.add("13");
    hoursList.add("14");
    hoursList.add("15");
    hoursList.add("16");
    hoursList.add("17");
    hoursList.add("18");
    hoursList.add("19");
    hoursList.add("20");
    hoursList.add("21");
    hoursList.add("22");
    updateAppointmentStartTimeHourComboBox.setItems(hoursList);
    updateAppointmentEndTimeHourComboBox.setItems(hoursList);

    ObservableList minList = FXCollections.observableArrayList();
    minList.add("00");
    minList.add("15");
    minList.add("30");
    minList.add("45");
    updateAppointmentStartTimeMinComboBox.setItems(minList);
    updateAppointmentEndTimeMinComboBox.setItems(minList);

我的问题是,当我试图预先填充该画面,我不能获得价值几小时或几分钟,以填充。

我能够得到LocalDateTime从我被任命为这里显示

LocalDateTime ldt = appointment.getStartDate().toLocalDateTime();
    LocalDate ld = ldt.toLocalDate();
    UpdateAppointmentDatePicker.setValue(ld);

    String tempStartHour = String.valueOf(ldt.getHour());
    updateAppointmentStartTimeHourComboBox.getSelectionModel().select(equals(tempStartHour));

但我无法得到组合框选择适当的价值和显示这一点。

如果我有tempStartHour="11"如何可以得到我的组合框选择和显示"11"从名单

combobox java list
2021-11-22 16:35:47
2
0

String.valueOf(ldt.getHour())

你是一个串的小时数。 通过默认,结果没有零的留的最重要的数字。

然后你试着匹配,未填充串针对串在哪里 0809 填充有一个零。

解决这个问题 的填充你提取的小时.

你的代码有其他问题。 一,需要选择一个默认小时时的输入是不在你的8至22范围。 对于另一个,你的最后一线的失败在语法,其中你不能穿 equals(tempStartHour) 作为一个参数。

提示:作为一个初学者,寻找其他代码的实例研究。

2021-11-22 17:19:18
0

假设你已经有的 LocalDate但你只需要转换到 LocalDateTime 使用价值的 ComboBox你可以使用 ComboBox<Number> 而不是的 ComboBox<String>和一个 NumberStringConverter 添加的前缀 0 单位数小时或几分钟(08:00 而不是的 8:0).

public class App extends Application {

    @Override
    public void start(Stage stage) {

        LocalDate date = LocalDate.now();

        ComboBox<Number> cbHourStart = new ComboBox<>();
        ComboBox<Number> cbHourEnd = new ComboBox<>();

        ComboBox<Number> cbMinuteStart = new ComboBox<>();
        ComboBox<Number> cbMinuteEnd = new ComboBox<>();

        NumberStringConverter converter = new NumberStringConverter("00");

        cbHourStart.setConverter(converter);
        cbHourEnd.setConverter(converter);
        cbMinuteStart.setConverter(converter);
        cbMinuteEnd.setConverter(converter);

        IntStream.rangeClosed(8, 22).forEach(cbHourStart.getItems()::add);
        IntStream.rangeClosed(8, 22).forEach(cbHourEnd.getItems()::add);

        IntStream.iterate(0, i -> i + 15).limit(4).forEach(cbMinuteStart.getItems()::add);
        IntStream.iterate(0, i -> i + 15).limit(4).forEach(cbMinuteEnd.getItems()::add);

        cbHourStart.getSelectionModel().select(0);
        cbHourEnd.getSelectionModel().select(0);

        cbMinuteStart.getSelectionModel().select(0);
        cbMinuteEnd.getSelectionModel().select(0);

        ObjectProperty<LocalTime> startTime = new SimpleObjectProperty<>();
        ObjectProperty<LocalTime> endTime = new SimpleObjectProperty<>();

        cbHourStart.getSelectionModel().selectedItemProperty()
                .addListener((obs, oldVal, newVal) -> startTime.setValue(
                        LocalTime.of(newVal.intValue(), 
                                cbMinuteStart.getSelectionModel().getSelectedItem().intValue())));

        cbMinuteStart.getSelectionModel().selectedItemProperty()
                .addListener((obs, oldVal, newVal) -> startTime.setValue(
                        LocalTime.of(cbHourStart.getSelectionModel().getSelectedItem().intValue(), 
                                newVal.intValue())));   

        cbHourEnd.getSelectionModel().selectedItemProperty()
                .addListener((obs, oldVal, newVal) -> endTime.setValue(
                        LocalTime.of(newVal.intValue(), 
                                cbMinuteEnd.getSelectionModel().getSelectedItem().intValue())));

        cbMinuteEnd.getSelectionModel().selectedItemProperty()
                .addListener((obs, oldVal, newVal) -> endTime.setValue(
                        LocalTime.of(cbHourEnd.getSelectionModel().getSelectedItem().intValue(), 
                                newVal.intValue())));

        startTime.addListener((obs, oldVal, newVal) -> 
                System.out.println("Start time: " + date.atTime(newVal)));

        endTime.addListener((obs, oldVal, newVal) -> 
                System.out.println("End time: " + date.atTime(newVal)));
    
        HBox hbStart = new HBox(5, cbHourStart, new Label(":"), cbMinuteStart);
        HBox hbEnd = new HBox(5, cbHourEnd, new Label(":"), cbMinuteEnd);

        VBox pane = new VBox(20, hbStart, hbEnd); 

        Scene scene = new Scene(new StackPane(pane));

        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch();
    }

}

注:

例如可以简化使用 binding 而不是添加更改的听众。 然而,他们刷新懒洋洋所以你需要添加更改的听众的性质,以迫重新计算的数值。

如果你已经在使用的听众的性质,你可以替换所有的变化的听众以前的例子有:

startTime.bind(Bindings.createObjectBinding(() -> 
        LocalTime.of(
                cbHourStart.getSelectionModel().getSelectedItem().intValue(),
                cbMinuteStart.getSelectionModel().getSelectedItem().intValue()), 
        cbHourStart.getSelectionModel().selectedItemProperty(), 
        cbMinuteStart.getSelectionModel().selectedItemProperty()));

endTime.bind(Bindings.createObjectBinding(() -> 
        LocalTime.of(
                cbHourEnd.getSelectionModel().getSelectedItem().intValue(),
                cbMinuteEnd.getSelectionModel().getSelectedItem().intValue()), 
        cbHourEnd.getSelectionModel().selectedItemProperty(), 
        cbMinuteEnd.getSelectionModel().selectedItemProperty()));
2021-11-22 17:13:29

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................