so just before we continue on I just wanted to go back to some of the things that we
use that we haven't seen before in the course firstly the to do items we
used this fx collection.observable array list and the reason
that we're using an observable array list comes back to our
controller and we were setting up our to do list view
and coming down here you can see to do list view scrolling down the set all command where
we are setting our to do items if we go in there and have a look
you could see that talks about the observable list an adding all element so we
need to have an array in a format with that used the observable ArrayList so
that's why we've created in our to do data a similar thing so we used the
fx collections.observable array list to create that from scratch and then we
append our to do items from there and in addition we've used a couple of things
that look a little bit different we've used path and files and we haven't
seen those before and you may have noticed when we're importing that it was java.
nio you can see nio.file.files and then .path.paths etc
and what they are if we just go back to a browser and I have opened up the package
java nio defines buffers which containers for data and provides an overview of other
nio packages and essentially down here you can see the package java.nio defines
buffers which containers for data and provides an overview which I've just said and a
central abstractions are buffers charset channels and selectors and
selection key so it's an alternative way to access data that is a more recent addition to
the Java language so we are using that just to be a little bit different to what you
saw in the files or the input or output section of the course but the
bottom line is it's pretty similar and I think this far into the course a lot
of that should be pretty readable to you so we're creating that observable
array list as I mentioned because ultimately from the javafx point of view we needed in
that format and fx collections
that package enables us to have an ArrayList in that format so we created
that and were just using this java nio functionality with path to get the
path to the file too load and then using files.newBufferedReader
which is pretty self explanatory what it's doing its creating new
BufferedReader for that particular file and similar also when we're writing as well
similar principle using that buffered writer this time and using files.newBuffered
writer to get it in that format so it should be pretty simple similar to what you've seen in
the past but just wanted to point those things and in case you're wondering why
we've done that and as I mentioned we are using a delimiter.tabdelimeter when we
are restoring an loading the values you can see on line 52 and here on line
78 we really wouldn't do that in a real world application we would probably
stored the items in XML format or perhaps even a database if we though theirs going to be
a lot of records but the purposes of this application we are creating it's just
fine to do it the way we have and what we're doing now is we talked about a
singleton and how we have to get access to the data so what we gonna do is
switch over the main Java class so lets do that
so what we want their is we want to add a stop method to our main class
and this method runs when the user exits the application usually by closing the main window
so we want to override that so....you can see that has come
from JavaFX.application and what we want to do in their is we
want to try and store our items so we need to run this once and we are going to store the items
that are hard coded in our controller so looking at our controller again we got all of these
items here that are hard-coded we're going to run this once closed down the
program which will then invoke the stop method in main.java and we go to write the
data out so that our data is now in a text file which ultimately we can start
accessing in future videos so we're going to delete the super and we need
to put it around a try catch block
so....
....
....so thats get our singleton instance and from their we put....
...
...so that's going to save it as I mentioned
to a text file that's when we close down the program for the first time and what we want
to do is temporarily
in our initialize method this is in our controller we want to temporarily get it to call so that it sets the
the items at that point because remember we're not creating this file
once ultimately we are gonna be creating this file every time we close the
application is gonna save all the changes to the files so we want one
mechanism now in our initialize method that is going to create the file just once a
temporary bit of code so that we can access it in a program but from then
on after we've created it we are gonna remove that line of code we'll leave it in here in the
stop method because every time we stop the application we want it to store our items so back
to the controller and the initialize method and just after adding the last item
after these five items we are going to just add that temporary bit of code....
....
...
...so that will create the file for us so we if we run this we should hopefully see the file get
created on the left hand side
and we will just run through their and check that its working seems to be working so we will close it down and you can see the file pop up
their todolistitems.txt and if have a look at that you can see we got our data and you
can see theirs tabs separating them we'll probably you can't see these tabs you can't
really see the tabs easily but there is a tabs in each of the key fields
their and it varies depending on how many characters were in each string
that has been recorded that's why it looks out of place but in terms of
readability when we read this back in it will be read and just fine because it
will recognize that these things here are actual tabs
ok so that's working at this point we've now got that file that contains our hard-coded items and
incidentally we didn't specify directory for the file so you can see that
it just stored the file in the root folder for our IntelliJ project and if
we wanted to we could just have a look at that in our finder...and you can
see that the file has been created in the root level for our
particular IntelliJ project ok so that's good so now we're gonna change the
code so that we load our to do list items from that file each time the
application starts rather than hard coding them in the controllers
initialization method so ultimately we are going to remove these from our initialize
method so what we are gonna do now is come back here to our main.java and we need to override another method we are
gonna override the init method which of course ultimately comes from the application
class so lets go ahead and do that so I'll just go below the stop and we'll override init and in their what we are going
to do is do a similar thing we are going to do to try catch so what I might do is
copy this code in our stop method and change a little bit of it and paste it into
the init overriding the Super which we don't need so instead of the store to do
items the init we're going to use or called rather the load to-do items so change that
to load so it loads our to do items from the text file that we created and everything else
we can leave just as it is so that is going to be called automatically by Java fx when we start
our application of course all it does is load the data in to the to do data instance
so now we've done that I'm gonna close down the run window we go back to our controller
class what we can do now is removed all the code that has got to do with creating and
storing the hard coded items and in addition we are going to remove this temporary
line as well as I'm just going to comment out all this out right up to and
including the set to-do items which we don't need to be in initialize anymore
that all been commented out and we shouldn't have any other errors and we still wanna
keep these other bits and pieces here we still want the set all for example to set our items
but instead of to-do items we need to change this and what that should be
calling our singleton so....
...
...and everything else should pretty much be the same we've commented out
the hard coded to do items because of course again they are coming out of the
file and now we've change it so its retrieving the data that was coming out of the file
that's currently in our to do data singleton so that's good to go
as well so going back to the to do data class because what we can do now is we have this method here the set to do items
we can now remove this and you can see that it is no longer been used because that was
just a temporary things that enabled us to sort of test this out why we're
getting to the stage of finishing the code off so we can just comment that
out now because we won't be using that anymore
again we only need that so the controller could store the hard coded items so lets now run
this we should find that everything will work as it did before
but now we're getting the data from the file so let's do that and we we're now getting
the data from the file and everything seems to be working fine but interestingly
enough you can see here that we've now found that the description or the detail field is
now all in uppercase you notice before where we had it coded in
over here it wasn't in upper case so we need to find out why is that doing that so
obviously the problems is come about since we've created our to do data class so lets go have a look in their and
most likely when we writing it we are storing it we made the typo in the
format and that is what happened
you can see string.format I've got percent lower case s a tab percent upper case s another tab and percent
lower case s so the uppercase s in the format has caused that problem so if we have a look at string.format in documentation and do
a search for that and will just check the documentation and then we'll just find
the right class.....ok so here we are I finally found it and if we have a look for format
we have a couple of options but the one we are looking at is the format one and we click on format string and we get some information to syntax for using that formatting and scrolling down you can see in general we got an s which produces a string but we've got an uppercase version which is varied so whats happening here in our code since I typed in
percent upper case s it converted everything to upper case so that should be case so I'll leave the code in their because what we'll
have to do to fix this we have to go back and put the code back with a hard-coded
values get that working and rewrite the file
it doesn't really matter if the details are in upper case because that's where we
add more items will be fixing that anyway and when that is written out again the problem
will be corrected for new ones so I won't fix the existing one because the existing
once is going to be written out again but in subsequent runs we should be good to go
just to confirm that the if we run this and you notice its still in upper case if close down the program where of course its
rewriting the items when the program's closed down run it again it's
still an uppercase
so even though we specified upper case there it's not going to change anything now that we
converted it back to lower case it just means it's gonna be written out as it is
effectively it's not going to be changed case its not going to try and change to what
it already is but new entries when we get to the stage of adding that to the program
that functionality will be written correctly as they typed so that's that
little bug fix sorted
so lets get back to working on the UI again and so what we want to do is now we've determine that
the basic applications is working is we want to provide a way for the user to add a new
to-do item to the list so we gonna add menu to our application
and will put it in the top position of the border pane so lets open up the main window fxml
and we are gonna used the top so come up here and put that at the top literally and....
....and if we run that now we won't see anything different and
that's because we haven't added into the menu bar but we have added the slot
for it and menu bar tag what we haven't done is add in any information in their so the
way to do that is we just type....
...and now
when we run it we should see a basic menu and you can see we got this file which at the moment we
click it nothing happens because we haven't specified
a sub menu to open so closing it down again is very equivalent to
actually clicking that on within IntelliJ so lets add an item so that
when we click it it does something in this case we're going to assign it to the text new so that when the
user wants to create a new to do item so within the menu text equals file on line 14 we
put in the items that we want to be in teh menu so....
....
...so if we run this we should see our mini menu click on file and you can see we
now got a new option which we havne't programmed it to do anything yet and
likewise if we click on exit and notice also that little separator line between the two
and bearing in mind the application will look a little bit different if you're
running on Windows or Linux but the same concept will apply in terms of the menu
options obviously pressing these items as I mentioned doesn't do anything at
this stage and that's because we're not listening for those events so what we
need to do is write an event handler for the new item so when the user presses new
we want to show them a dialog that they can use to create a new item so we start doing
that in the next video