Home > Software design >  Selecting a specific value from a list Java
Selecting a specific value from a list Java

Time:11-23

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.

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 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.

If i have tempStartHour = "11" how can i get my combobox to select and display "11" from the list

CodePudding user response:

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.

Fix this by padding your extracted 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. For another, your last line fails in syntax, where you cannot pass equals(tempStartHour) as an argument.

Tip: As a beginner, seek out other code examples to study.

CodePudding user response:

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).

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. 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()));
  • Related