Question
The goal in this assignment is to exercise JavaFX GUI development and multi-threading. Long duration and short duration tasks will run simultaneously such that long
The goal in this assignment is to exercise JavaFX GUI development and multi-threading. Long duration and short duration tasks will run simultaneously such that long duration task will not block the GUI or the short duration task.
The GUI is as follows:
In this GUI, you will have 2 textfields Sum and Increment and 2 buttons: Sum and Increment.
The initial value in the textField Increment is 0. In the textField sum you will always sum numbers from 1 to 50. And textfield sum will show the intermediate values while the sum is being executed.
Sum button click will trigger summing up values in a loop from 1 to 50 and it uses Thread.sleep at each iteration to slow down the task. The increment button increments the value written in the Increment textField by 1. It is our short duration task.
1) create a javaFX FXML Application, and open your FXML file using the SceneBuilder. Add the components as shown above.
2) create 2 actions in your controller file as didClickSumButton and didClickIncrementButton.
3) save your controller, and using the popup menu, call Compile File.
4) go to the SceneBuilder, and bind your methods to the buttons.
5) Implement body of didClickIncrementButton
6) Implement body of didClickSumButton
6.1) In the sum button, sum numbers from 1 to 50 in a background thread
6.2) To achieve, define a Runnable Object, and in its run function, perform the sum
operation. Please kindly pay attention that we will have 1 GUI thread and 1 background thread
running.
- If we do not use a background thread, then the long-running job will block our GUI thread.
- If we use a background thread, then updating the GUI means accessing some objects in another thread which may cause synchronization issues.
- To overcome synchronization issues of GUI update from a background thread, you can use the following logic:
Platform.runLater(new Runnable() { public void run() { // Update GUI in this function } });
- By using the above code piece, you will not cause any synchronization issues, and different threads update the same object safely.
6.3) After completing the definition of your Runnable object (assume its name is task), then you need to start it using:
// Run the task in a background thread Thread backgroundThread = new Thread(task); // Terminate the running thread if the application exits backgroundThread.setDaemon(true); // Start the thread backgroundThread.start();
6.4) To prevent the user from pressing the Sum button after the summation thread started, disable sum button in the beginning of the didClickSumButton method
6.5) At the "finally" section of the try block of "run" function of Runnable Object defined in 6.2, enable the sum button.
AnchorPane I TextField oK Button Sum I TextField oK Button Increment AnchorPane I TextField oK Button Sum I TextField oK Button IncrementStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started