NBKRIST-JavaHub : JavaFX Q&A
🏠 Back to Home

💼 Expert View

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.

🎯 JavaFX Event Handling and Basics (Beginner Level)

1. What is JavaFX?
JavaFX is a GUI framework in Java used for building rich desktop applications. It provides tools for 2D/3D graphics, media, UI controls, CSS styling, and event handling mechanisms.
2. What are the key components of a JavaFX application?
The three main components are:
Stage – The top-level window.
Scene – Holds all UI components (nodes).
Nodes – The individual visual elements (like Button, Text, or ImageView).
3. Explain the lifecycle of a JavaFX application.
Every JavaFX program extends the Application class and follows this sequence:
1️⃣ init() – initializes variables/resources.
2️⃣ start(Stage primaryStage) – builds and shows the UI.
3️⃣ stop() – cleans up resources before exit.
4. What is an Event in JavaFX?
An event represents an action or interaction in the UI, such as a mouse click, key press, or window resize. JavaFX uses event objects to describe and handle these interactions.
5. What are the main types of Events?
Common event types include:
ActionEvent – triggered by buttons or menu items.
MouseEvent – when the mouse is clicked, moved, or dragged.
KeyEvent – keyboard input events.
WindowEvent – window actions like close or minimize.
6. What is Event Source and Event Target?
Event Source is the node that triggers the event (like a Button).
Event Target is the node that receives and handles the event.
7. How to handle a Button Click event using a regular class?
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.
8. How do you handle Mouse Events in JavaFX?
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());
9. Explain a real-time example using Mouse Events.
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.
10. How to detect mouse entered or exited events?
Rectangle box = new Rectangle(100, 50, Color.LIGHTGRAY);
box.setOnMouseEntered(e -> box.setFill(Color.LIGHTGREEN));
box.setOnMouseExited(e -> box.setFill(Color.LIGHTGRAY));
11. How to handle keyboard events without lambdas?
class KeyHandler implements EventHandler<KeyEvent> {
    public void handle(KeyEvent e) {
        System.out.println("Key Pressed: " + e.getCode());
    }
}

scene.setOnKeyPressed(new KeyHandler());
12. What is Scene Builder in JavaFX?
Scene Builder is a drag-and-drop tool for designing JavaFX user interfaces visually.
• It automatically generates FXML files.
• It allows quick design without writing code.
• Helps separate UI layout from application logic.
• Increases productivity and reduces syntax errors.
13. How do you display text in JavaFX?
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).
14. How do you display an image in JavaFX?
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.
15. What is the difference between Stage and Scene?
Stage is the main application window, while Scene is the content area that holds and displays all UI components (nodes) inside that stage.