简体   繁体   English

从列表中选择特定值 Java

[英]Selecting a specific value from a list Java

I am trying to create a scheduling program where I can update customer appointment times.我正在尝试创建一个可以更新客户预约时间的调度程序。 I am able to save my appointments but updating them has been a bit confusing.我可以保存我的约会,但更新它们有点令人困惑。

I have 2 lists for Hours and Minutes that I am putting in comboboxes as shown below.我有 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);

My issue is when I am trying to prepopulate the screen, I cannot get the value of the hours or minutes to populate accordingly.我的问题是当我尝试预填充屏幕时,我无法获得相应填充的小时或分钟的值。

I am able to get the LocalDateTime from my appointment as shown here我可以从我的约会中获取 LocalDateTime,如下所示

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

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

But i cannot get the combobox to select the appropriate value and display it.但我无法将 combobox 到 select 获得适当的值并显示它。

If i have tempStartHour = "11" how can i get my combobox to select and display "11" from the list如果我有 tempStartHour = "11" 我怎样才能让我的 combobox 到 select 并从列表中显示“11”

String.valueOf(ldt.getHour())

You are making a string from the hour number.您正在从小时数制作一个字符串。 By default, the result has no zeroes to the left of the most significant digit.默认情况下,结果的最高有效位左侧没有零。

Then you try to match that unpadded string against strings where 08 and 09 are padded with a zero.然后,您尝试将该未填充的字符串与0809用零填充的字符串进行匹配。

Fix this by padding your extracted hour .通过填充提取的 hour来解决此问题。

Your code has other problems.您的代码还有其他问题。 For one, you need to pick a default hour for when the input is not within your 8-22 range.一方面,当输入不在 8-22 范围内时,您需要选择一个默认小时。 For another, your last line fails in syntax, where you cannot pass equals(tempStartHour) as an argument.另一方面,您的最后一行语法失败,您不能将equals(tempStartHour)作为参数传递。

Tip: As a beginner, seek out other code examples to study.提示:作为初学者,请寻找其他代码示例进行学习。

Assuming you already have the LocalDate , but you only need to convert to LocalDateTime using the values of the ComboBox , you can use a ComboBox<Number> instead of ComboBox<String> , and a NumberStringConverter to add a prefix 0 for single-digit hours or minutes ( 08:00 instead of 8:0 ).假设您已经有LocalDate ,但您只需要使用ComboBox的值转换为LocalDateTime ,您可以使用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();
    }

}

Note:笔记:

The example can be simplified using binding instead of adding change listeners.可以使用binding而不是添加更改侦听器来简化示例。 However, they are refreshed lazily so you'll need to add change listeners to the properties to force a recalculation of values.但是,它们会延迟刷新,因此您需要向属性添加更改侦听器以强制重新计算值。

If you are already using listeners for the properties, you can replace all the change listeners of the previous example with:如果您已经为属性使用了侦听器,则可以将前面示例中的所有更改侦听器替换为:

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()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM