Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The goal in this assignment is to exercise JavaFX GUI development and to run a task in a background thread. The GUI is as follows:

The goal in this assignment is to exercise JavaFX GUI development and to run a task in a background thread.

The GUI is as follows:

image text in transcribed image text in transcribed

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 didClickAddOneButton.

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 didClickAddOneButton

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

the effect of AI in customer service

Answered: 1 week ago

Question

What is management growth? What are its factors

Answered: 1 week ago

Question

2. What type of team would you recommend?

Answered: 1 week ago