so our application working fine but let's just say that it takes 10 seconds
to fit a large group of records from a slow database and of course currently we've
got a delay in our method on line 20 where the delays just one second or a 1000
milliseconds now perhaps we are fetching these records over the internet which
takes time because we're using a background thread to get the records the
user interface remains responsive to the user but it may look like nothing's
happening
so the user may well wonder you know if the application is working correctly or
not
so what we could do is report the progress of the background task to the
user and tasks that we've been using make it easy for us to do just that
so the first step is to add a progress bar control to the user interface let's
open the sample.fxml file and just below the list view lets add our
progress bar so select progress bar and we can do.....
we'll call it progress bar so that we can refer to that in code and we'll put
this on.....and put it in column.....
.....so if we run this as is without making any other changes
you can see that we have got a progress bar and it's adding like an
indeterminate one so this is the default state of the progress bar and it's
indeterminate because nothing is reporting progress to it so to get that
working what we need to do is close this down and declare the progress bar in the
controller we go back to that and will declare it to below the task up here
actually we will do it below the listview to be consistent so it's going to be....
....
....and then what we want to do then is put the change in our
method that we've defined for the task in the core method so what we're going
to do is we're going to declare this observable list a little bit differently
so what we're going to do is start off and below well actually what we're going to
do is remove thread.sleep first and will change it so that we got.....
....and then we're going to just grab the names
here from the final list employee that we added previously i'm just going to paste
those in between the block they're just fix it up a little bit alright so
their names and then what we want to do is we still need our observable list
that no longer needs to be final so i'm going to change that and i'm just going
to make it an observable arraylist without any parameters at the moment
delete that now we're going to create a little loop here so that we can add each
name and we can simulate a delay between each so we are going to put in the
loop here that says....6 being the number
of Records
....so we're adding the name from the string array into the
observable list then we also want to do....
......
leave in the return that returns a list of employees alright so let's talk about
to what we've done so we're calling the update progress method after adding
each name that method takes two parameters the current progress and the
maximum progress so we're adding six names so we've set the maximum progress
as the second parameter on line 37 to 6 now sometimes the mas can change as the
task progresses which is why we pass it every time we update the progress for the
current progress the first parameter we passed a number of names we've added and
then we have to add one because we are counting from zero but now that we've done that
there is one more step we need to add to do we are updating the progress but we
haven't tied the progress to the progress bar yet once again we're going to
use data binding to link the progress bar to the tasks progress and we do that
by binding the progress bars progress property to the tasks progress property
so to do that we add this below the data binding we set up for list view....
....
....that's binding them together
then and we're leaving the listview in there as well as it was before
let's just run this and see what it does
alright we still got an indeterminate progress bar there until we actually
click on the buttons want to click on get employee names now and you can see
that work then you can see that that slowly incremented with that small delay
each time a record was or each time an entry was added and then at the end they
all appeared on the screen so if I close this down
and run it again we can see that in operation again the moment indeterminate
the progress bar click on get employee names and you can see that sort of go
through like it was doing something and then eventually we got the full list
displayed at the end now in a real-world application would only display the
progress bar when we start a background task we wouldn't show the progress bar all
the time and so it wouldn't be constantly animating as it did until it
receives progress from the task will see how to do that in a minute and we can
also display messages from the tasks so lets actually add a label underneath the
progress bar so we go back to our fxml and below the progress bar add a
label.....
....
...then we'll go back to our
controller and we obviously need to declare it in there as well so...
....and make sure when we import
that that was selecting the right one this case it's the javafx.seen.
control the correct one
we just need to annotate it .fxml okay so that's good to go
now we will update the task so that it updates the message property each time
it adds an employee name to the list so to do that we're gonna go down just
before the update progress and after the employees.add we're we are naming the
or adding i should say the employee name in there i'm going to do....
...
....now we call set text to set of labels text when we
do so we're actually setting the labels text property so knowing that can you
guess how the label is going to display the message from the task so if you
guessed by binding its text property to the tasks message property you're
actually spot on
so let's go ahead and do that and we'll do this again doesn't really matter
particularly where we put but we will put it just below the progress bar so...
....
...that binds the two together so the messages
or message rather will be displayed on the screen so when we run this now we should
see the label update each time a task adds a name to the list
so let's just run it and check that does work you can see down the bottom that
was working quite nicely and the labels showing the last name that was added in
this case Bob McDonald so javafx is designed to use properties in this way
so when you want to update the UI control whenever the value of something
else has changed or set do check the documentation to see if what you want to
update and the source value can be bound together because it's a great way
of doing it and you are leaving the javafx framework to do the heavy
lifting so before we leave this topic let's take a look at the service class which
we can use to execute a task on a background thread so it's there to help
developers use background threads correctly in a UI application now we can
start and cancel the service and unlike a thread we can also restart the service
so let's change our example to use the service
the first thing we need to do is add a subclass of the service class to the
controller so we have to implement a method called create task in our sub
class instead of creating our task instance in the initialize method we're
going to create it in the services create task
method instead so let's end the video here now and will start work on
this on that employee service class in the next video