w3resource

JavaFX Events and Event handling exercises


Java Events and Event Handling Exercises [16 exercises with solution]


[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

Handling mouse and keyboard events:

1. Display Mouse Coordinates on Move

Write a JavaFX program application that detects and displays mouse coordinates when moved over the application window.
Click me to see the solution

2. Detect Left Mouse Click

Write a JavaFX program that listens for mouse clicks and displays a message when the left mouse button is clicked.
Click me to see the solution

3. Display Key Pressed by Keyboard

Write a JavaFX application that responds to keyboard input and displays the key pressed.
Click me to see the solution

4. Change Background with Key Combination

Write a JavaFX application that changes the background color of a window based on a combination of keys pressed (e.g., Ctrl+Alt+O changes the background to orange).
Click me to see the solution

Action Events and Event Listeners:

5. Button Click Console Message

Write a JavaFX form with a button. Implement an event listener to display a message to the console when the button is clicked.
Click me to see the solution

6. Simple Calculator with Button Events

Write a JavaFX program to build a simple calculator with digit buttons (0-9) and basic arithmetic operator buttons (+, -, *, /). Implement action listeners for each button (digits and operators) to perform calculations.
Click me to see the solution

7. Shopping Cart with Item Buttons

Write a JavaFX application that simulates a shopping cart. Add items with buttons, and implement action listeners to update the cart when an item is added.
Click me to see the solution

8. Login Form with Action Listener

Write a JavaFX login form. Implement an action listener on the "Login" button to validate user credentials.
Click me to see the solution

Event Propagation and Event Filters:

9. Nested Panes Event Filtering

Write a JavaFX application with multiple nested panes. Implement event filters to capture and log the event flow when an action occurs.
Click me to see the solution

10. Tree UI with Event Propagation

Write a JavaFX application that creates a tree-like JavaFX UI with branches and leaves. Use event propagation to determine which branch or leaf was clicked on.
Click me to see the solution

11. Game Event Propagation and Filtering

Write a JavaFX program that builds a game where events propagate through various elements. Use event filters to handle game events efficiently.
Click me to see the solution

12. Drawing App with Shape Events

Write a JavaFX program to develop a drawing application with shapes. Implement event filters to detect and respond to user interactions with shapes.
Click me to see the solution

Creating Custom Event Handlers:

13. Traffic Light Custom Events

Write a JavaFX application with custom events representing different traffic light signals (e.g., Red, Yellow, Green). Create custom event handlers to handle color change messages.
Click me to see the solution

14. Virtual Pet with Custom Event Handlers

Write a JavaFX application for a virtual pet. Implement custom events and event handlers for actions like feeding, playing, and petting the virtual pet.
Click me to see the solution

15. Chat App with Custom Messaging Events

Write a JavaFX program that creates a chat application. Define custom events for sending and receiving messages and implement custom event handlers for message handling.
Click me to see the solution

16. Stock Trading Simulator with Events

Write a JavaFX program to develop a stock trading simulator. Create custom events for stock price changes and use custom event handlers to simulate trading decisions.
Click me to see the solution

Java Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.