考虑一下:
ComboBox<T>.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent ae) {
// Do something.
}
}
//...
public void triggerDefinedComboBoxEventHandler() {
// "Click/fire/trigger" ComboBox<T>, in order to perform
// action described in it's "setOnAction()" body.
}
实现上述功能的最佳方法是什么?不幸的是,我找不到javafx.scene.control.ComboBox<T>
组件的任何内容,它与javafx.scene.control.Button.fire()
方法相同或至少类似。因此,到目前为止我使用的解决方案如下所示:
public void doSomething() {
// Do something.
}
//...
ComboBox<T>.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent ae) {
doSomething();
}
}
//...
public void triggerDefinedComboBoxEventHandler() {
doSomething();
}
......但似乎不够优雅。
答案 0 :(得分:2)
Event.fireEvent(comboBox, new ActionEvent());