ok so now we can write the code for the
action handler for our contact dialogue ok so now we can write the code for the
fxml now if you recall we did action handler for our contact dialogue
something similar in the to do list fxml now if you recall we did
application so the code i'm about to something similar in the to do list
show you should be familiar now we want application so the code i'm about to
the dialogue to be modal and tied to the show you should be familiar now we want
main window of the application so we the dialogue to be modal and tied to the
need to assign a fx id to the border main window of the application so we
pane to achieve that need to assign a fx id to the border
let's get back to our main dot FXML and pane to achieve that
for the border pane we need to add an ID... let's get back to our main dot FXML and
... for the border pane we need to add an ID...
....and then go back to our ...
controller class again this is the ....and then go back to our
controller for the main dot FXML we first controller class again this is the
need to add a field... controller for the main dot FXML we first
.... need to add a field...
... ....
... ...
alright so now that we've done that we ...
can add the code for the action handler alright so now that we've done that we
let's go ahead and do that in here can add the code for the action handler
in the show add contact dialogue going let's go ahead and do that in here
to start with a dialog... in the show add contact dialogue going
... to start with a dialog...
... ...
...we're getting this ...
error here a little bit of a tricky one ...we're getting this
it's actually got to do with the error here a little bit of a tricky one
language level so i go into my module it's actually got to do with the
settings going to project this should language level so i go into my module
actually be set to settings going to project this should
jdk 8 if I do that the errors disappears actually be set to
so we've got so far lets actually now jdk 8 if I do that the errors disappears
add the buttons for our dialogue so it's so we've got so far lets actually now
going to be dialogue dot get dialogue add the buttons for our dialogue so it's
... going to be dialogue dot get dialogue
... ...
...so let's go ...
through what we just added here so i ...so let's go
started by creating a dialogue instance through what we just added here so i
and assigning the main window as its started by creating a dialogue instance
parent and I assigned the parent on line and assigning the main window as its
twenty and we're getting that parent parent and I assigned the parent on line
from the border pane and then set the twenty and we're getting that parent
dialogue title and I've then loaded the from the border pane and then set the
fxml and set the dialogues content to dialogue title and I've then loaded the
what we've defined in the fxml file the fxml and set the dialogues content to
contact contact dialogue .fxml and what we've defined in the fxml file the
then i added the ok and can cancel buttons contact contact dialogue .fxml and
and then we actually open the dialog then i added the ok and can cancel buttons
on line 36 and added the if statement and then we actually open the dialog
will check when the user presses ok or on line 36 and added the if statement
cancel if they pressed ok then we want will check when the user presses ok or
to process the information they're cancel if they pressed ok then we want
provided and add the new contact to our to process the information they're
contacts list before we do that let's provided and add the new contact to our
actually just run the application just contacts list before we do that let's
to make sure that this part of the code actually just run the application just
work to make sure that this part of the code
ok we got our menu up here and click work
on the menu click on ADD and there's our ok we got our menu up here and click
little while dialogue pane that's popped on the menu click on ADD and there's our
up little while dialogue pane that's popped
so obviously that's working we can see up
the fields that are in the dialog and so obviously that's working we can see
click on cancel to close it down so the fields that are in the dialog and
that's good we can continue on click on cancel to close it down so
alright so what what do we actually want that's good we can continue on
to do with the information that the user alright so what what do we actually want
provides in that dialogue box we want to do with the information that the user
actually create an instance of the provides in that dialogue box we want
contact class and add that to the actually create an instance of the
contacts list now because we set things contact class and add that to the
up to use data binding that's all we contacts list now because we set things
should have to do in other words we up to use data binding that's all we
don't have to explicitly change the UI should have to do in other words we
at all but this raises the question don't have to explicitly change the UI
of when we want to create an instance of at all but this raises the question
contact data class of when we want to create an instance of
now we haven't actually created that contact data class
instance yet now we could do it in main now we haven't actually created that
dot java as part of the javafx instance yet now we could do it in main
application lifecycle but since this is dot java as part of the javafx
a small application and everything the application lifecycle but since this is
user will want to do can be accessed a small application and everything the
from the main window i think it makes user will want to do can be accessed
sense for the controller to create the from the main window i think it makes
contact data instance let's do that in sense for the controller to create the
the initialize method that the javafx contact data instance let's do that in
runtime calls when it creates the the initialize method that the javafx
controller so we go back to our runtime calls when it creates the
controller class and let's add the controller so we go back to our
initialize at the top and what we want controller class and let's add the
to do first is also add an instance of our initialize at the top and what we want
contact data class so... to do first is also add an instance of our
...and then we want to contact data class so...
add out initialize method... ...and then we want to
... add out initialize method...
... ...
alright so we've got a contact data ...
instance at this point but we haven't alright so we've got a contact data
read in any save contacts but we'll do instance at this point but we haven't
that after we've created the instance so... read in any save contacts but we'll do
...right now that after we've created the instance so...
they're aren't any but that's ok we'll add ...right now
a few new entry shortly they're aren't any but that's ok we'll add
the last thing we want to do in the a few new entry shortly
initialize method is to initialize the the last thing we want to do in the
tables data if you recall we added an fx initialize method is to initialize the
ID to the table view already and tables data if you recall we added an fx
we just needed to declare the field in ID to the table view already and
this controller so go ahead and do that we just needed to declare the field in
... this controller so go ahead and do that
... ...
now that we've done that we can ...
initialize the table view in the now that we've done that we can
initialize method we do that below the initialize the table view in the
load contacts and we do that with contact initialize method we do that below the
table.... load contacts and we do that with contact
.... table....
alright so this is a good point in our ....
program to run again to make sure things alright so this is a good point in our
are still working...are still working program to run again to make sure things
also the dialog box still work we are not are still working...are still working
getting any errors or exceptions and also the dialog box still work we are not
obviously there's no saved records so getting any errors or exceptions and
we are seeing exactly as we saw the last obviously there's no saved records so
time we ran it i'm going to close that we are seeing exactly as we saw the last
down time we ran it i'm going to close that
alright so now we can work on handling a down
new contact now we need the information alright so now we can work on handling a
that's been entered by the user but at new contact now we need the information
the moment that isn't accessible from that's been entered by the user but at
the main controller now we have to get the moment that isn't accessible from
the information using a content the main controller now we have to get
controller and provide a way for the the information using a content
main controller to get the new contact controller and provide a way for the
so let's add a get new contact method to main controller to get the new contact
the contact controller and it's going to so let's add a get new contact method to
gather the information provided the the contact controller and it's going to
user create a new contact gather the information provided the
instance and then return the instance to user create a new contact
the caller will go to our contract instance and then return the instance to
controller class we need to enter fields the caller will go to our contract
first... controller class we need to enter fields
... first...
... ...
...and now on to the ...
public method and that will be public ...and now on to the
... public method and that will be public
... ...
.... ...
...so that code fairly ....
straightforward i had to declare all the ...so that code fairly
text fields then inside the get new straightforward i had to declare all the
contact method we retrieve all the text fields then inside the get new
information from the dialog box we contact method we retrieve all the
create a new contact return that to the information from the dialog box we
calling method now in a real-world create a new contact return that to the
application you want to verify the input calling method now in a real-world
at the very least you want to check that application you want to verify the input
the required fields weren't empty and would at the very least you want to check that
probably require first name last name the required fields weren't empty and would
and phone number and maybe have notes to probably require first name last name
be optional and if we wanted to be and phone number and maybe have notes to
really user friendly we only enable be optional and if we wanted to be
the ok button in the dialog when really user friendly we only enable
the three required fields weren't empty but the ok button in the dialog when
we can also just check the contents here the three required fields weren't empty but
but i won't actually add that now I will leave that as we can also just check the contents here
an exercise for you to add validation but i won't actually add that now I will leave that as
alright so let's go back to the main an exercise for you to add validation
control so when the users pressed ok will alright so let's go back to the main
get a reference to the contact control so when the users pressed ok will
controller and call it get new contact get a reference to the contact
method and will then add the new contact controller and call it get new contact
to the contacts list and the UI should method and will then add the new contact
hopefully automatically update so to the contacts list and the UI should
let's go back to our controller hopefully automatically update so
and we come down here to this process of let's go back to our controller
show the dialog box also got this code and we come down here to this process of
here if result is present and result. show the dialog box also got this code
get equals button okay that means here if result is present and result.
hopefully enter the data and click OK get equals button okay that means
meaning they want to save the field hopefully enter the data and click OK
save the meaning they want to save the field
information so what we want to do there save the
is we do... information so what we want to do there
... is we do...
... ...
ok we are using the reference to fxml ...
loader that we defined here on line ok we are using the reference to fxml
36 and we know that we are getting the loader that we defined here on line
getting access to the controller that's 36 and we know that we are getting the
associated with that fxml file and then getting access to the controller that's
we're going to do contact... associated with that fxml file and then
... we're going to do contact...
... ...
...fairly ...
straightforward we've got access to our ...fairly
contact controller so that we can access straightforward we've got access to our
this get new contact method to retrieve contact controller so that we can access
the contents from the dialogue field create a this get new contact method to retrieve
new contact record and ultimately the contents from the dialogue field create a
save that to our via contact data class new contact record and ultimately
we're going to do one more step here save that to our via contact data class
we're going to save the new contact to we're going to do one more step here
the xml file by saving all the contacts we're going to save the new contact to
now this isn't the most efficient way to the xml file by saving all the contacts
do it so we could write the file when a now this isn't the most efficient way to
user closes the application or perhaps do it so we could write the file when a
set at periodic intervals but since user closes the application or perhaps
we're going to be dealing with a small set at periodic intervals but since
number of contacts will do this whenever we're going to be dealing with a small
the user adds edits or delete a contact number of contacts will do this whenever
that way they'll never lose any of their the user adds edits or delete a contact
work so we'll do this on the line below that way they'll never lose any of their
that... work so we'll do this on the line below
we are calling save contacts method after that...
they've added the new contact to the we are calling save contacts method after
list they've added the new contact to the
alright so let's run this and see what list
happens alright so let's run this and see what
alright so let's create a new entry happens
contacts... alright so let's create a new entry
.... contacts...
... ....
click on OK and we can see the contact ...
appear in the table and just to confirm click on OK and we can see the contact
this let's close the application and run appear in the table and just to confirm
it again to see whether our contact is this let's close the application and run
loaded run again and success you can see it again to see whether our contact is
we now got our contact loaded run again and success you can see
successfully loaded from the xml file we now got our contact
when the application and restarts successfully loaded from the xml file
so let's add three more contacts just when the application and restarts
so theirs a few more there to work with so let's add three more contacts just
so... so theirs a few more there to work with
.... so...
...if you did make a ....
mistake and you wanted to change things ...if you did make a
you could actually make a change to the mistake and you wanted to change things
contacts . xml file because at the you could actually make a change to the
moment we haven't got away in the contacts . xml file because at the
application to editing these so if I close moment we haven't got away in the
down we can double-click this contacts to application to editing these so if I close
XML which should be appearing in the down we can double-click this contacts to
project pane and there's the entries XML which should be appearing in the
that are being added automatically by project pane and there's the entries
the code in the contact data class that are being added automatically by
alright so I'm going to finish the video the code in the contact data class
here in the next video what we're going to do alright so I'm going to finish the video
is start looking at the code to edit here in the next video what we're going to do
our contacts so see you in the next video is start looking at the code to edit