在另一个 ComboBox 中选择项目后动态更新 Combobox - JavaFX
Dynamically update Combobox after selecting item in another ComboBox - JavaFX
我有一个 JavaFX 表单,其中包含两个组合框,其中填充了以 15 分钟为增量的开始时间和结束时间。我试图让结束时间组合框在用户 select 开始时间时动态地重新填充选项,这样用户就不可能在开始时间之前 select 结束时间同时保留用户的 selection 如果用户已经 selected 了一个仍在开始时间之后的结束时间。
在测试代码是否有效时,我已经能够让两个框正确填充并正确保留用户的 selection,但是,当用户 selects 新的开始时间。如果我使用 onMouseClicked,它会在您单击组合框时触发事件,而不是在您创建 selection 时触发事件,如果使用 onMouseExit 事件它会起作用,但会在烦人的延迟之后发生。
当 ComboBox 中的项目被 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 时调用的控制器方法
@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));
}
}
我确信我遗漏了一些明显而简单的东西,但我似乎看不到它。如果我错过了另一个问题,我为重复的问题道歉,但我在这里和其他网站上浏览了几篇文章,但没有弄清楚。如有任何帮助,我们将不胜感激。
也许您正在寻找 onAction?
// --- 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; }
我有一个 JavaFX 表单,其中包含两个组合框,其中填充了以 15 分钟为增量的开始时间和结束时间。我试图让结束时间组合框在用户 select 开始时间时动态地重新填充选项,这样用户就不可能在开始时间之前 select 结束时间同时保留用户的 selection 如果用户已经 selected 了一个仍在开始时间之后的结束时间。
在测试代码是否有效时,我已经能够让两个框正确填充并正确保留用户的 selection,但是,当用户 selects 新的开始时间。如果我使用 onMouseClicked,它会在您单击组合框时触发事件,而不是在您创建 selection 时触发事件,如果使用 onMouseExit 事件它会起作用,但会在烦人的延迟之后发生。
当 ComboBox 中的项目被 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 时调用的控制器方法
@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));
}
}
我确信我遗漏了一些明显而简单的东西,但我似乎看不到它。如果我错过了另一个问题,我为重复的问题道歉,但我在这里和其他网站上浏览了几篇文章,但没有弄清楚。如有任何帮助,我们将不胜感激。
也许您正在寻找 onAction?
// --- 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; }