在另一个ComboBox中选择项目后,动态更新Combobox-JavaFX [英] Dynamically update Combobox after selecting item in another ComboBox - JavaFX

查看:210
本文介绍了在另一个ComboBox中选择项目后,动态更新Combobox-JavaFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaFX表单,其中有两个组合框,其中以开始"和结束"时间的15分钟为增量填充时间".我正在尝试使结束时间"组合框在用户选择开始时间时动态地填充选项,以便用户无法在开始时间之前选择结束时间,同时保留用户的选择(如果用户已经选择了)选择的结束时间仍晚于开始时间.

I have a JavaFX form with two combo boxes populated with Times in 15 min increments for Start and End times. I am trying to get the End time combo box to dynamically repopulate with options when the user selects a start time so that it is not possible for the user to select an End time before the start time while preserving the user's selection if the user has already selected an end time that is still after the start time.

在测试代码正常工作时,我能够使两个框正确填充并正确保留用户的选择,但是,当用户选择新的开始时间时,我无法正确触发事件.如果我使用onMouseClicked,则它会在您单击组合框时触发该事件,而不是在您进行选择时触发该事件,如果使用onMouseExit事件,它将起作用,但会令人讨厌地延迟.

I have been able to get both boxes to populate correctly and correctly retain the user's selection when testing that the code works, however, I cannot get an event to fire correctly for when the user selects a new start time. If I use the onMouseClicked it fires the event when you click on the combo box not when you make a selection, it will work if using the onMouseExit event, but after an annoying delay.

在ComboBox中选择一个项目后,如何才能正确启动?

How can I get this even to fire correctly when an item in the ComboBox is selected?

FXML

 <ComboBox id="Start Dropdown" fx:id="cbStart" onMouseClicked="#handleSelectStart" prefWidth="150.0" GridPane.columnIndex="5" GridPane.rowIndex="4">
       <GridPane.margin>
          <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
       </GridPane.margin>
 </ComboBox>
 <ComboBox id="End Dropdown" fx:id="cbEnd" prefWidth="150.0" GridPane.columnIndex="8" GridPane.rowIndex="4">
       <GridPane.margin>
          <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
       </GridPane.margin>
 </ComboBox>

由动作侦听器调用handleSelectStart时调用的控制器方法

Controller method called when handleSelectStart is called by the action listener

@FXML
private void handleSelectStart(MouseEvent event){
    //Get the currently selected Start time from Start ComboBox
    LocalTime time = LocalTime.parse(cbStart.getValue(), timeDTF);

    //Store the current Selected End time for later comparison
    String currentEnd = cbEnd.getSelectionModel().getSelectedItem();

    //Clear out existing options from End Combo Box ObservableList
    availEndTimes.clear();

    do{
        availEndTimes.add(time.format(timeDTF));
        time = time.plusMinutes(15);
    } while(!time.equals(LocalTime.of(17, 15)));

    availEndTimes.remove(0);

    if(availEndTimes.contains(currentEnd)){
       cbEnd.setItems(availEndTimes);
       cbEnd.getSelectionModel().select(currentEnd);
       //setValidEndTimes();
    } else {
       cbEnd.setItems(availEndTimes);
       cbEnd.getSelectionModel().select(availEndTimes.get(0));
    }
}

我确定我缺少明显而简单的内容,但似乎看不到它.如果我错过了另一个问题,我为重复的问题表示歉意,但是我在这里和其他站点浏览了几篇文章,却没有弄清楚.任何帮助将不胜感激.

I am sure I am missing something obvious and simple but I cannot seem to see it. If I missed another question I apologize for the duplicate question but I looked through several articles here and on other sites without figuring it out. Any assistance would be greatly appreciated.

推荐答案

也许onAction是您要找的东西?

Maybe onAction is what you're looking for?

// --- On Action
/**
 * The ComboBox action, which is invoked whenever the ComboBox
 * {@link #valueProperty() value} property is changed. This
 * may be due to the value property being programmatically changed, when the
 * user selects an item in a popup list or dialog, or, in the case of
 * {@link #editableProperty() editable} ComboBoxes, it may be when the user
 * provides their own input (be that via a {@link TextField} or some other
 * input mechanism.
 */
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() { return onAction; }

这篇关于在另一个ComboBox中选择项目后,动态更新Combobox-JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆