Skip to content

Instantly share code, notes, and snippets.

@manuel-mauky
Last active February 10, 2016 09:52
Show Gist options
  • Save manuel-mauky/b5c52dc5ba091f436f1e to your computer and use it in GitHub Desktop.
Save manuel-mauky/b5c52dc5ba091f436f1e to your computer and use it in GitHub Desktop.
ComboBox Eventhandling when ENTER is pressed
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.function.BiConsumer;
import java.util.function.Function;
public class Testpp extends Application {
public static void main(String... args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
ComboBox<String> cb = new ComboBox<>();
cb.getItems().addAll("Test", "hello", "world");
cb.addEventFilter(KeyEvent.KEY_RELEASED, event -> System.out.println("combobox filter"));
cb.addEventHandler(KeyEvent.KEY_RELEASED, event -> System.out.println("combobox handler"));
TextField tf = new TextField();
tf.addEventFilter(KeyEvent.KEY_RELEASED, event -> System.out.println("textfield filter"));
tf.addEventHandler(KeyEvent.KEY_RELEASED, event -> System.out.println("textfield handler"));
VBox root = new VBox(cb, tf);
Scene scene = new Scene(root);
scene.addEventFilter(KeyEvent.KEY_RELEASED, event -> System.out.println("scene filter"));
scene.addEventHandler(KeyEvent.KEY_RELEASED, event -> System.out.println("scene handler"));
primaryStage.setScene(scene);
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment