ok so now that we've actually loaded and assign the dialogue pane or at least written
the code to do so we now need to add our buttons to the dialogue or more
accurately to the dialogue pane and we want our dialogue to have an OK and add a Cancel
button and JavaFX provides a way for us to specify those buttons
and interestingly enough we can also create custom buttons but we won't do that here
so to add ok and cancel buttons what we need to do is create instances of the button type
class and the button type constructor as you see accepts the text we want on the button
and button type so lets just go ahead and add this so this is going to be after the try in here
we are gonna put...
....
...so at this point
we are ready to show our dialogue now theirs two methods that we could use here to show
it we could use show as the method or show and wait and the difference
between them is that show and wait will bring up a blocking dialogue and show
will bring up a non-blocking dialogue so what does this actually mean well the
show method will return immediately after showing the dialogue in other
words it won't wait for the user to press the OK and Cancel buttons so we have to
write code that would occasionally pull the dialogue for a result and thinking
about this its really not what we want to do here we want to show the dialogue and then we
want our event handler
to be suspended while the user interacts with the dialogue and when the user's finished
our event handler will received the result of the dialogue and then resumed executing
and results of this will be which button will the user pressed so as a result we
want to use the show and wait method and that's generally what you want to use
with dialogue boxers so interested in which button the user pressed because if they
pressed ok and of course we add the to do item they entered in
the actual dialogue itself but if they press cancel we don't do any adding at all so we
don't do anything so for now we're just gonna print out which button they pressed so that
we can see if our code is working up until this point so lets go and do that so
after this ok and cancel we type....
....
...
....
....so lets run that so click on
file and click on new and nothing happens so why is that nothing happened
well we have one more step what we haven't done anything we haven't associated
the event handler with the menu item we need to go back to our main windows FXML and
do that so we're telling it which code to execute when we click on the new button
in this case so closed it down and go back to our main window FXML and where we defined our menu items
you can see that we got this new item here on line 18...we need to add
something extra we put....
...so we are telling it what method to invoke if they select
that option so now if we run it this time we click on file and new we get an error that means I've made a
mistake somewhere so let's just check that out so go back and see where the error was
and the error is actually down here that show new item dialogue controller
on line 59 so if we just have a look at that we are getting a problem with main border
pane and just go back and make sure that we specified it ok in the FXML
...and you can see what I've done there I've actually many top main border pain which was a
pain in did so that should be pane
we didn't really pay much attention to that did I because when I type pain
you can see it was basically in a different color and you can't see a dramatic
difference but if we hover over it we can see that theirs a warning
unresolved reference so I should have actually checked that so....should fix that the
warnings is gone and if we go back to our controller.java that's now highlighted
that was in gray before so if we run
this again we should find that it
will work now for us hopefully so we click on file new and this time we got a dialog box
that's good we got our dialogue showing and if I click ok we get the message their ok was
pressed file new cancel pressed and interestingly enough if we just open
that again file new notice that the ok boxes color differently and that's because its
the default button that means if we press ENTER while the dialogue is up the dialog
is going to interpret this is pressing the OK button so now we got that open and if I just press enter we'll return
you notice how that has been invoke and OK pressed has come up on the screen
ok so the code is working great so far so now let's write the code that will gather the
information from the dialogues controls when the user clicks the ok button but how do we
actually do that now the FXML file that defines the dialogue pane and all its
controls is associated with another controller so we can't create instance
variables for our dialogues controls in our main window's controller what we need is
a way to get the dialogues controller so that when the user presses the OK button we
can then ask the dialogues controller too process the data so to get the
controller we have to change the way we load the FXML so instead of using the
load method we are gonna do it in several steps what we'll do is first create an instance of the FXML
loader class and then we'll tell it the location of the FXML we want to
actually load then we will go ahead and load the FXML
so lets close it down....
so here is the code we are using at the moment in the try block
so we are gonna change this around a little bit so just above the try in the show new item dialogue
we going to type in....
....
...
....
....so now we can get our controller from the
FXML loader instances you can see their but first we need to write the
method we want to call so lets switch to our
dialogue controller Java class come over here so what we need to do
is go back to our to do item dialogue we need some IDs for the
various fields so let's add some ID's so the first one is a text field
because we need to be retrieving the values so...
....
...so lets now go back to our dialog controller and
we will declare these fields in there so...
....
...
....
...so in here what we want to do is we want to gather
the user's input creating a new to do item and we add it to in our to
do data instance
so lets go ahead and do that so...
....
...
....
...so that will be the only thing we need to
do from this point of view to process the results after the OK button was written
but we need to go back to our to do
class and add this to do item methods so lets go back and do that....
...and in the bottom will add that.....
.....
...so we are gonna do....
....
..ok so going back to dialogue controller that should now be fixedo you can see the error went away
because the method has now been added so now we've done that we have to call the process
results method from the event handler in the main windows controller so to see
what I mean will go back to our main controller where we implemented the code at the moment we've got
just putting out a message if ok was pressed so this is where we need to actually
process something so we need to first get the controller and we do that with...
....
...and that will then process that for us so
let's run this....
...add a new one....and we got an error....and the error is on Controller.java on line 63 and its got to do with the dialogue
setting the content from the FXML loader so what I'm going to do is I'm going to finish the
video here now because its getting quite long
when we come back we'll fix this error and then will actually look at making sure
that when the to do item is added in the dialogue that it has been added successfully
and its also added successfully in our list view as well so see you in the next video