w3resource

JavaFX UI Components Exercises and Solutions

JavaFx User Interface Components Exercises [21 exercises with solution]

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

Buttons and Labels:

1. Write a JavaFX application with a button that changes the label text when clicked. Initially, the label should display "JavaFX!" When the button is clicked, change the label text to "Button Clicked!".
Click me to see the solution

2. Write a JavaFX program with two buttons. When the first button is clicked, the label should display "Button-1 clicked." Similarly, clicking the second button should change the label to "Button-2 clicked".
Click me to see the solution

3. Write a JavaFX application with a label and a button. Initially, the label should be empty. When the button is clicked, set the label text to a random number between 1 and 10.
Click me to see the solution

TextFields and PasswordFields:

4. Write a JavaFX application with a text field and a button. When text is entered in the field and the button is pressed, display the entered text in a label.
Click me to see the solution

5. Write a login form using JavaFX with username and password fields. Add a "Login" button. When the button is pressed, validate the entered username and password, and display a success or failure message.
Click me to see the solution

6. Write a JavaFX program with two password fields. When the user enters a password in both fields and presses a button, check if the passwords match and display a message accordingly.
Click me to see the solution

ChoiceBox and ComboBox:

7. Write an JavaFx application that creates a ChoiceBox with a list of colors. When a color is selected, display it in a label or change the background color of a rectangle to the selected color.
Click me to see the solution

8. Write a JavaFX application that uses a ComboBox to choose a month (e.g., January, February). When a month is selected, display the number of days in that month.
Click me to see the solution

9. Write a JavaFX application that builds a country selection form using a ChoiceBox or ComboBox. Populate the dropdown with a list of countries, and when a country is selected, display its capital in a label.
Click me to see the solution

CheckBox and RadioButton:

10. Write a JavaFX application with multiple CheckBox options (e.g., "Red," "Green"). Display a label showing the selected options when checkboxes are clicked.
Click me to see the solution

11. Write a JavaFX preference form using RadioButton options for choosing a Boolean value (e.g., "True," "False"). Display a message based on the selected value.
Click me to see the solution

12. Write a JavaFX food ordering application using CheckBox options for different food items (e.g., "Burger," "Pizza"). Calculate the total cost based on the selected items.
Click me to see the solution

ToggleButton and ToggleGroup:

13. Write a JavaFX application with two ToggleButtons that act as radio buttons. When one is selected, the other should be deselected. Display a label indicating the selected button.
Click me to see the solution

14. Write a JavaFX preference form with ToggleButtons to choose a color (e.g., "Red," "Green"). Ensure only one color can be selected at a time and display the chosen color.
Click me to see the solution

15. Write a JavaFX application for choosing a wireless mode using ToggleButtons (e.g., "Bluetooth," "WiFi," "hot-spot"). Highlight the selected wireless mode.
Click me to see the solution

Sliders and ProgressBars:

16. Write a JavaFX program with a slider that controls the radius of a circle. Display the width value using a label.
Click me to see the solution

17. Write a JavaFX media player interface with a slider to control playback progress. When the slider is moved, update the video or audio progress. Integration of media libraries is required to create a full-fledged JavaFX media player interface with video and audio playback. So create a simplified example that demonstrates how to implement a JavaFX interface with a slider to control progress. Use a dummy label to represent playback progress.
Click me to see the solution

18. Write a JavaFX program to build a download manager application with a ProgressBar to show download progress. When a download is in progress, update the ProgressBar accordingly.
Click me to see the solution

TextArea and ListView:

19. Write a JavaFX application with a TextArea and a button. When text is entered in the TextArea and the button is pressed, display the entered text in a ListView.
Click me to see the solution

20. Write a JavaFX program to develop a to-do list application using a ListView. Allow users to add, remove, and edit items in the list.
Click me to see the solution

21. Write a JavaFX program to build a chat application with a ListView for displaying messages. Allow users to enter and send messages using a TextArea. Messages should be added to the ListView when sent.
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.