Supondo que você já tem o LocalDate
, mas você só precisa converter para LocalDateTime
usando os valores da ComboBox
, você pode usar um ComboBox<Number>
em vez de ComboBox<String>
e um NumberStringConverter
para adicionar um prefixo 0
para um único dígito horas ou minutos (08:00
em vez de 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();
}
}
Nota:
O exemplo pode ser simplificada usando binding
em vez de adicionar alteração ouvintes. No entanto, elas são atualizadas de forma preguiçosa de modo que você precisará adicionar os ouvintes para as propriedades para forçar o recálculo de valores.
Se você já estiver usando ouvintes para as propriedades, você pode substituir todas as mudanças ouvintes do exemplo anterior com:
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()));