so right now the user can add to do items but they can't edit or delete them
as I mentioned in the last video and in addition to what I said in the last
video coding the edit case is really similar to the add case and will even be
using the same UI if we did that and because of that I'm gonna leave the
edit case as an exercise for you to do what I'm gonna do is show you the Delete
case now so we are gonna add the ability to be able to delete a to-do
item from our list in terms of implementing that we could add a delete item
to our file menu in much the same way that we did for the new item for the new to
do item but in the case for delete item it's probably not the ideal place
to add it there no it was ok for the add new to-do item case in that case because we
don't need any other information from the user in order to add a new item
because of course all that does is opens the add new to do item dialogue and from
there they enter the details when the user deletes an item it's different in
that we need to know which item that we want to delete
now we could assume the user wants to delete the selected item and that's probably what
do the user will expect but what we should do is make it crystal clear to the user that
we are going to be deleting that selected item so let's have a menu popup when the user
right clicks on an item in a list now this type of menu is different to the menu
that we already implemented in our program and this new type is called a context
menu and the reason this called that is when the is because the menu
that appears depends on the item that's been right clicked on and let's have a
look at IntelliJ sat context menu just to see what I mean so if you come over here to a
project in the top left window right click that you can see that is a menu that is the context
menu that's been brought up by IntelliJ and you can see there's quite a
few options their a significant amount of options but if you come down to say
external libraries right click that you can see we've clearly got a different set of
options so that is what a context menu is the options that come up are relative to
what you've selected or what you have right clicked so we are gonna do
something similar in this application so what you need to do is go to our controller.java
and that's because we're adding the context to our main window
let's go ahead and do that so we need to create a field first and I've already added it here
private context menu list context menu so you need to add that to the java file
so we added it their as a field and we also in the initialize method we
want to initialize it as well we are going to initialize that by typing...
....to initialize it when the initialize method is executed
so now we've done that we need to create the delete menu item that's going to be part
of this context menu and associated an event handler so to do that we are going to type...
....
....and next
we need to add the code the event handler that is going to be processed when we exit when the right
click happens so to do that we do....
....
.....and you can see its overridden the handle
method which were gonna be using and in there that's where we going to choose
what to delete to add the code its quite easy we do...
....
...
...and that's all we need to add for our
context menu and we will implement that delete item method
but before you do that what we need to do is add to delete item menu item
itself to our context menu and we do that by calling get items in our context
menu and we add the delete item using the add all method so here we can do that so...
....
...so that has now added that option to that context menu so
next let's go and add this delete item method so come down here
in a bit further actually I'll just put it down the bottom jsut to be in order of what we've done before so we
gonna create that method so...
....
now we could just go ahead and delete the selected item as soon as the user
clicks the delete menu item but it will be nice here because it's a deletion to
confirm that deletion before we go ahead and delete it you know because sometimes
users click to wrong item or click on something and then sort of reconsider the
action so we learn earlier that JavaFX provides us with a few canned dialogues
for common use cases and we are gonna use what's called a confirmation dialog to confirm
that the user really was to go ahead and to in this case delete an item
now the alert class which has only been available since Java 8u40 is the
class that we gonna use here to create one of those javafx
canned sort of pre created dialogues now when we create an instance of alert what
we have to do is pass in the type of dialogues that we want to create and this
five choices that they give us a confirmation error information warning and
none and then based on the type that we choose the alert constructor will
initialize some of those instances properties for us based on obviously
the type dialogue we choosing and you might be wondering why would you want to pass a none
well the none case just means that there's no properties that get initialized and in other words
you be wanting to set each property yourself manually so in this case we want the user to
confirm the deletion so what we want is a confirmation dialog sort of are you sure sort of
thing so in that case we are gonna code that in that matter so start here in the delete item method and
type...so confirmation is the
one that is going to come up automatically for us and ask the user for confirmation
before we delete so next we want to set properties that determine what the dialogue will
is gonna display on the screen so we need to set the title header
text and content text properties so what we'll do is we'll set the title to
deleted to do items so its comes up really literally at the top and we want our header
text to provide information about the selected to do item sort of as a
recap for the User so the know what they are deleting in other words so the user
can see which item their ultimate gonna delete if they go ahead and will set the
header texts to delete item followed by the selected items short description
and then in the content area what we'll do is we'll ask the user if they
are sure they want to delete the item and that way there is no ambiguity and the user will hopefully
know exactly what's happening here so to go ahead and do that so...
....
.....
...remembering that items was pass to this method so we can get the details directly from that and that's obviously the current selected
item and let's set the content text as well so....
....
now in terms of buttons we don't have to set which buttons we want here and
that's because the alert class will provide those buttons automatically
based on the type that we passed with the alert type in this case
confirmation in the case for confirmation I'm sure you've seen a dialogue before many times
applications that you run on your computer theirs an OK and Cancel button so all that's
left to do now is really show the dialogue so we get
that working by putting...
....
....so when the user clicks on one of the buttons in the
dialogue closes then we need to obviously at that point check which button they
pressed and if they pressed ok we need to delete the selected item and
in the case of canceled we don't do anything so do that test here we put....
.....
....
and that should be it so that's all the code we need to handle the menu to process that menu
selection when right click is selected and show the confirmation dialog box so then
we need to go back to our to do data class we need to add that deletion
the delete to do items so we do that with....
....so that's all that
we need for that method and going back to our controller class the delete to do items is now valid
so we are pretty close to being done there but theirs actually one more step to do so
if you think about it we created the context menu we associated handler to it and we've written the
handlers code but what we haven't done is we haven't associated the menu with the list view
in any way now since this is a context menu
meaning its going to appear when the user right clicks on a particular item or cell in this case it
makes sense to associate the menu in our cell factory now in a cell factory class
we will add the code that associates the context menu after we've created the cell
now we only want non empty cells to have a context menu in this case so let's go ahead
and associated the context menu to our cell factory so we need to come up to our initialize
method in our controller and we need to add some code down here just
below the returns cell so we need to actually add
this code to do that association we are gonna add it down here
I'm gonna type in...
..and what I'm going to here is I'm going to do something we haven't done before on the
course and their is a separate section of the course dealing with this
I'm going to use an anonymous function also known as a lambda expression and
i'm not going to looking into to much detail about this but keep in mind there is a complete section in the
course about lambda expressions or their will be by the time you watching
that section may or may not already exist but this is just an alternative way to write a
function so I want to show you first part of that anyway so the parameters for that is we
need to specify them first so....
....
...
....
...so its an alternative way of adding a list of class adding
a listener method by using an anonymous method but again we'll talk about the
lambda expressions in a separate section
of the course the bottom line is now that we've added a listener for the cells empty
property where the cell becomes non empty the context menu is gonna be associated
with it the one that we've created in this video and it does become empty we then
remove the association by setting the cells context menu to null so lets now run this and see if it works
we should be able to select something like doctors appointment right click that
we now get our context menu showing so click on delete now we can see we got our confirmation dialogue
propping up and you can see the item short description is displaying....
....question marks has come automatically from the the type that we
specified and also the cancel and ok buttons have been added other than that we got our title and the other text
we've added showing up on the screen so its all seemingly working ok and click on
cancel just to confirm or cancel it and try it again right click it delete
this time click ok we can see to the left the to do item did get deleted and the list
got refreshed
now notice we didn't write any code to remove the item from the list and that's because we're using data
binding and the list view notice that the item was deleted from the collection
then updated itself accordingly
now another way we could let the user to delete item is to listen for the delete key
so when the user selects an item and presses the delete key we could do
exactly what we did when the right click is activated and select delete from the
context menu so we'll look at adding that functionality so adding the delete functionality so when you press delete
on the keyboard it basically pops up the context menu will do that in the next
video