As an industry professional with years of experience in enterprise Java projects, I strongly recommend mastering these types of JavaFX and event-handling questions. Companies like Infosys, Wipro, Tech Mahindra, and TCS often assess how well you understand GUI fundamentals, event-driven programming, and real-time problem-solving in interviews. Remember — consistent practice with these scenarios not only boosts your interview confidence but also builds the kind of hands-on thinking required in real-world software development.
Application class and follows this sequence:init() – initializes variables/resources.start(Stage primaryStage) – builds and shows the UI.stop() – cleans up resources before exit.class MyButtonHandler implements EventHandler<ActionEvent> {
public void handle(ActionEvent e) {
System.out.println("Button clicked!");
}
}
Button btn = new Button("Click Me");
btn.setOnAction(new MyButtonHandler());
This approach separates logic from the UI code — useful for clarity and reusability.class CircleClickHandler implements EventHandler<MouseEvent> {
public void handle(MouseEvent e) {
System.out.println("Mouse clicked at: " + e.getX() + ", " + e.getY());
}
}
Circle circle = new Circle(50, Color.CORNFLOWERBLUE);
circle.setOnMouseClicked(new CircleClickHandler());Pane pane = new Pane();
pane.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Circle c = new Circle(e.getX(), e.getY(), 10, Color.DODGERBLUE);
pane.getChildren().add(c);
}
});
📘 This demonstrates dynamic UI creation using mouse coordinates.Rectangle box = new Rectangle(100, 50, Color.LIGHTGRAY);
box.setOnMouseEntered(e -> box.setFill(Color.LIGHTGREEN));
box.setOnMouseExited(e -> box.setFill(Color.LIGHTGRAY));class KeyHandler implements EventHandler<KeyEvent> {
public void handle(KeyEvent e) {
System.out.println("Key Pressed: " + e.getCode());
}
}
scene.setOnKeyPressed(new KeyHandler());Text msg = new Text(100, 120, "Welcome to JavaFX!");
msg.setFont(Font.font("Arial", 20));
msg.setFill(Color.DARKBLUE);
This displays styled text on the scene at position (100,120).Image img = new Image("file:photo.png");
ImageView view = new ImageView(img);
view.setFitWidth(200);
view.setPreserveRatio(true);
💡 Tip: Always use setPreserveRatio(true) to maintain aspect ratio.