Reputation: 3119
I'm making a combobox that is filtered by typed text, and shows the drop down whenever text is typed.
I found this example which works very well. I have modified it slightly so the dropdown appears when text is entered.
However, when I type a few letters and then press ctrl + A to select all text in the TextField
, it does not select all of the text if the dropdown is visible. Something else is consuming that hotkey.
Here is the MCVE code:
import com.sun.javafx.scene.control.skin.ComboBoxListViewSkin;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class MCVE extends Application {
public void start(Stage stage) {
HBox root = new HBox();
ComboBox<String> cb = new ComboBox<String>();
cb.setEditable(true);
ObservableList<String> items = FXCollections.observableArrayList("One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten");
FilteredList<String> filteredItems = new FilteredList<String>(items, p -> true);
cb.getEditor().textProperty().addListener((obs, oldValue, newValue) -> {
final TextField editor = cb.getEditor();
final String selected = cb.getSelectionModel().getSelectedItem();
Platform.runLater(() -> {
if ( !editor.getText().isEmpty() ) {
cb.show();
} else {
cb.hide();
}
if (selected == null || !selected.equals(editor.getText())) {
filteredItems.setPredicate(item -> {
if (item.toUpperCase().startsWith(newValue.toUpperCase())) {
return true;
} else {
return false;
}
});
}
});
});
cb.setItems(filteredItems);
root.getChildren().add(cb);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Here are a few solutions I've tried. None of them work. It seems that the issue is that JavaFX has reserved the hotkey Ctrl + A
and will not let me grab onto it.
(this one works if the key is D
, but not A
((ComboBoxListViewSkin)cb.getSkin()).getDisplayNode().addEventFilter( KeyEvent.KEY_PRESSED, keyEvent -> {
if ( keyEvent.isControlDown() && keyEvent.getCode() == KeyCode.A ) {
cb.getEditor().selectAll();
}
});
(this one also works if the key is D
, but not A
cb.setOnKeyPressed( ( KeyEvent e ) -> {
if ( e.isControlDown() && e.getCode() == KeyCode.D ) {
cb.getEditor().selectAll();
}
});
Upvotes: 2
Views: 860
Reputation: 51525
The behaviour is the same for a plain combo (that is without any filtering) and looks like a bug: ctrl-A is eaten by the ListView in the dropDown. To work around, you can install the eventFilter on the list, f.i. in a onShown handler - at this time the skin is installed:
cb.setOnShown(e -> {
ComboBoxListViewSkin<?> skin = (ComboBoxListViewSkin<?>) cb.getSkin();
ListView<?> list = (ListView<?>) skin.getPopupContent();
list.addEventFilter( KeyEvent.KEY_PRESSED, keyEvent -> {
if (keyEvent.isControlDown() && keyEvent.getCode() == KeyCode.A ) {
cb.getEditor().selectAll();
}
});
cb.setOnShown(null);
});
This is working in all versions (8 and 9+). For 9+ the bug is worse in that all navigation inside the editor is disabled (aka: list eating left/right as well).
Upvotes: 2