Search code examples
javaeventscomboboxjavafxkeyevent

javafx keytyped event not working


I have a JAVAFX editable combobox on which key typed and key pressed events are not firing while key released event is firing. However, If I change the combobox to textfield, it works. FXML:

 <ComboBox fx:id="combo_box" editable="true" layoutX="311.0" layoutY="194.0" prefHeight="26.0" prefWidth="300.0" promptText="Enter your name" onKeyTyped="#keyAction"  />

FXMLController:

public void keyAction(KeyEvent event)
{
System.out.println("Works");
}

Help?


Solution

  • I found something which works. You can use the "getEditor" method of the combobox, to get the KEY_TYPED event works. Put this code in your controller :

    this.combo_box.getEditor().setOnKeyTyped((KeyEvent e) -> {
         System.out.println("Works");
    });
    

    Hope it helps