I am implmenting a cusom component where the user can type into a TextField
and a TableView
is pops up then the user can look for items in that table.
I have a problem with the standard KeyEvent
s of the TextField
like Ctrl+A or Home. After the pane with the TableView
pops up, those key events are not working anymore.
I have checked if the TextField
lost the focus but it doesn't, and if I set an EventFilter
to see what is happening, it shows that those events are triggered but I see no effect on UI. Even the popup's setHideOnEscape
is not working.
Here is a simple code to verify it:
The fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="textField.Controller">
<TextField fx:id="textField" prefWidth="400"/>
</AnchorPane>
The controller:
public class Controller implements Initializable {
@FXML
private TextField textField;
@Override
public void initialize(URL location, ResourceBundle resources) {
Popup popUp = new Popup();
TableView<Object> table = new TableView<>();
table.prefWidthProperty().bind(textField.widthProperty());
popUp.getContent().add(table);
popUp.setHideOnEscape(true);
popUp.setAutoHide(true);
// To see if the KeyEvent is triggered
textField.addEventFilter(KeyEvent.ANY, System.out::println);
textField.setOnKeyTyped(event -> {
if(!popUp.isShowing()){
popUp.show(
textField.getScene().getWindow(),
textField.getScene().getWindow().getX()
+ textField.localToScene(0, 0).getX()
+ textField.getScene().getX(),
textField.getScene().getWindow().getY()
+ textField.localToScene(0, 0).getY()
+ textField.getScene().getY()
+ textField.getHeight() - 1);
}
});
}
}
And the main:
public class Main extends Application {
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
AnchorPane pane = loader.load();
primaryStage.setScene(new Scene(pane, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is the console output
KeyEvent [source = TextField[id=textField, styleClass=text-input text-field], target = TextField[id=textField, styleClass=text-input text-field], eventType = KEY_PRESSED, consumed = false, character = , text = a, code = A]
KeyEvent [source = TextField[id=textField, styleClass=text-input text-field], target = TextField[id=textField, styleClass=text-input text-field], eventType = KEY_TYPED, consumed = false, character = a, text = , code = UNDEFINED]
KeyEvent [source = TextField[id=textField, styleClass=text-input text-field], target = TextField[id=textField, styleClass=text-input text-field], eventType = KEY_RELEASED, consumed = false, character = , text = a, code = A]
KeyEvent [source = TextField[id=textField, styleClass=text-input text-field], target = TextField[id=textField, styleClass=text-input text-field], eventType = KEY_PRESSED, consumed = false, character = , text = , code = CONTROL, controlDown, shortcutDown]
KeyEvent [source = TextField[id=textField, styleClass=text-input text-field], target = TextField[id=textField, styleClass=text-input text-field], eventType = KEY_TYPED, consumed = false, character = , text = , code = UNDEFINED, controlDown, shortcutDown]
KeyEvent [source = TextField[id=textField, styleClass=text-input text-field], target = TextField[id=textField, styleClass=text-input text-field], eventType = KEY_RELEASED, consumed = false, character = , text = a, code = A, controlDown, shortcutDown]
KeyEvent [source = TextField[id=textField, styleClass=text-input text-field], target = TextField[id=textField, styleClass=text-input text-field], eventType = KEY_RELEASED, consumed = false, character = , text = , code = CONTROL]
Any idea why are those events consumed even if it says they aren't?