Reputation: 4677
I have a javafx.scene.control.ChoiceDialog
object in my JavaFX project. When the dialog opens, I want to set the focus to the underlying ComboBox
. However, I can't figure out how to get the underlying ComboBox
control. With a TextInputDialog
, there is a method called getEditor()
. Is there any way (other than reflectively accessing the private comboBox
member) to get the underlying ComboBox
?
Upvotes: 3
Views: 2359
Reputation: 6952
To set the focus to the ComboBox
you can use a dirty hack, which triggers updateGrid
of the ChoiceDialog
after it is shown:
ChoiceDialog<String> choiceDialog = new ChoiceDialog<>("test", getData());
choiceDialog.showingProperty().addListener((ov, b, b1) -> {
if (b1) {
choiceDialog.setContentText("");
}else {
choiceDialog.setContentText(null);
}
//or
if (b1) {
Node comboBox = choiceDialog.getDialogPane().lookup(".combo-box");
comboBox.requestFocus();
}
});
Obtaining the ComboBox
instance using the style class is useful to override the default keyboard navigation:
final var options = (ComboBox) getDialogPane().lookup( ".combo-box" );
options.setOnKeyPressed( ( event ) -> {
// When the user presses the down arrow, open the drop-down. This prevents
// navigating to the cancel button.
if( event.getCode() == KeyCode.DOWN && !options.isShowing() ) {
options.show();
event.consume();
}
} );
When the above changes in place, pressing the down arrow key will open the combo box instance instead of navigating to the Cancel button.
Upvotes: 4