so at the end of the previous video I mentioned that some event handlers can
take parameters so if you wanted to do that we can actually add an action even
parameter to our existing event handle methods so lets go and do that and see how that works
so I'm just gonna add them in and I'm gonna call it action event which is the name of the class we are going to be using....
...now the reason we might want to do this so I just accept that import the reason why we
might want to do this is we might want to use lets say the same event handler for more than one control
and if we're going to do that we need to know which control the user interacted
with so if we add this parameters to a method and then called the get source method we can
actually figure out which control was actually used so it makes our code
more concise as we don't certainly have to create an event handler for every
each and every control so lets look at doing that so I've added the action even as a parameter and
to actually use it we can just type in something just to get some information from that
action events so I could do something like.....
.....
which will tell us ultimately which control was actually pressed
the button so if we do that and run it
and you can see the following button was pressed button say hello
and that has give us some information about the button or in fact that was a button the style class tells us
that its a button and its like
the standard toString method here that's been used and that's just meant to be
like a memory location where that's stored in memory that it should change each time
just to confirm that if we run it again notice how its a completely different memory location so
that's working for one button and we can use that to figure out which button
ultimately this coming from and if you remember the say hello go back to our fxml
the text = say hello so it was using that to determine or to show us which button it actually
referring to that's because its using the standard toString method from the get source
which returns an objects we are actually just getting the toString representation of
what's been returned from e.getSource so let's actually add a second button just
to be able to see how this works in practice so back into our fxml and add
the second button......
....
.....
....
.....
.....and actually lets add an ID to that as well what we are gonna do is lets
change this code around a little bit now so I've got an ID at the moment for the text field
but we haven't actually got ID's defined for the buttons so lets also do that
so we can start using those as well so we are gonna put....
.....
....
...so lets go back now
and adjust the controller code for our event handler and we're going to put another field here we
got name field we also going to add a hello button and I buy button so......
.....
.....bearing in mind this have to be an exact
match between what's we've assigned here the name here hello button and buy button to what is in the fxml
has to be hello button and buy button like so and next is to change the code a little bit now so
that we can actually bit smarter in what we're doing so I'm gonna change this and
delete that for now and change this to....
.....if we have got the hello button in other words the event
handler has been invoked for the hello button we are going to print....
...and if
that's the case we need to print out....
...so you now see we've got one method that is handling the
events or the event handler for two buttons now and just to confirm this
works I can type Tim.....
say hello we got hello tim appearing in the bottom of the screen and click on say bye bye time so you can see
that's working as well now we could have just created another event handler
and associated it with a buy button and in this case probably would have made more
sense but I just wanted to show you this technique how it can be useful depending on the
functionality that you are trying to achieve to have one event handler handling many controls and not just the one method
if we have a really long event handler and most of the code would be duplicated if
we were to add a similar event handler to the same type of control and obviously reusing
the same hander is an option and it's probably a good way to go to reduce the
amount of code duplication and we could also assign different handler and put the common
code into another method
and have both event handlers call that method the best option would depend on the specific case
on what you're trying to actually achieve and just to be clear you can see that on
line 18 and on line 20 we are using the .equals for the .getSource to
actually figure out whether we're actually working with the hello button or
the buy button and that is a pretty cool bit of functionality that checks that and that enables us to
actually write code that is specific for that particular control that appears
in our layouts so now we know the basics of event handling just look at some other controls
and the events that we may want to handle in our code so right now we don;t
check to see if the user's type anything into the text field now our application will not crash
if we press one of the buttons when the field is empty
just to demonstrate I'll delete that and just say hello, and then nothing
and the bye nothing so it doesn't crash if its empty but let's make sure the field isn't
empty now we could do this by adding code to our event handler if the field is empty
we could do nothing or perhaps we could inform the user by displaying an error message
but its better to prevent the user from doing something that they shouldn't and to
verify user input and report an error
so in our case it's better for us to disable the buttons when the field is empty and only
enable the buttons when something has actually been type into the field that way we can be
sure that when our event handler is called the field isn't empty and the user won't do
something only to be told they can't do it until they've done something else if that makes sense in other words
preventing the user from making mistakes in the first place will result
in a much better experience for users now when we first run the application we
actually want our buttons to be disabled because we know that the field will
initially be empty the text field and when buttons are disabled they won't
respond to user input and their appearance will be different so that the
user knows the buttons won't do anything now when the user types something into the
text field of course we want to enable the buttons at that point so this means
we want to be be notified whenever a character is type into the text field
in can you guess what we want to do here we want to listen for every time a
character is typed into a text field and run an event handler when that
happens so first let's write our event handlers so what we are going to do is we're going
to listen for the on keypress event so will call our event handler handle
key released lets close that down so again we are gonna listen for the on key realeased event
and write a handler for that so I'm gonna close it down and making sure I am at controller.java lets write a handler so.....
..so how are we going to go about this well firstly lets get
the contents so its gonna be string text equals name field.gettext then we put....
.....
.....so as you can see we
can call the is empty method to actually check whether its empty or not and
that's should have been a proper or their and
we also using .trim as well so we are basically ignoring anyone white spaces that's why we're
doing that additional tests so if either of those is true in other words it comes
back empty in either case
we are going to disable the buttons accordingly otherwise if its set to false which means it's not empty
disabled buttons is gonna be set basically set to false and that means that when we
are the disable is going to be false which effectively means it is going to enable it for us and this may seem
inefficient but we have to check every time the user types a character into the
field we could also accept a key event parameter and that would tell us which key
was typed into the field but we don't really need to know in this case which
key was we only care about whether the field is empty or not and I just need to annotate
that.....so that's gonna handle that for now but we also need to disable
the buttons when the application first runs so what I'm going to do add a initialize
method to our controller now the JavaFX runtime calls this method when it's
initializing the UI and the method can't have any parameters and it must be public
and will also have to annotate it with the @fxml
annotation so I'm gonna put that at the top....
and the actual code will be quite simple its going to just be.....
...so in other words we do want to
enable it we do want to disable it I should say the buy button...
...so again that is going to be called automatically our method when the
our application is being initialized so the final step we now
need to do is to basically associate the event handler that we just wrote on line 32
to our text field and the fxml so lets do that....
....
..so this is after a key is pressed but when it's actually released
when you take your fingers off the key we want to call....and run it again to check if it works
you can see the buttons are correctly disabled initially and when we type something
notice how they actually got enabled so you can see our code and our event handlers is actually
working nicely but if we then backspace to delete the T what actually happens
you can see that the buttons do disable so again that's working well
but what happens if we type a blank into the text field in other words space so
we click on a space or nothing actually works here correctly and that's because
I've got that second test in play but I were to changed that code and come
back down here
changes this here and comment that out temporarily so we are now testing to
see whether the field is empty
click on that run it again put a T in there if that works press delete and its goes away and if I know press space
notice how now its now enabled again so in other words it's not until in this
particular case what we want to do is make sure that a space isn't entered isn't
valid effectively that's what the second method call is doing is basically
trimming results getting rid of any white space which obviously includes spaces
and only then
enabling that to run so again we check that T, Delete press space
no matter how many spaces are press its considered as white space and it won't enable which is the functionality that we are looking for here
ok so I'm gonna leave the video here now in the next video we're going to add a
checkbox and start looking at creating some event handlers to cater
for that see you in the next video