Passing information between Scenes with Javafx

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay welcome back to the series on creating gooeys in Java so there's a few things that we want to do with our with our GUI at this point I've got a little cleanup items to take care of maybe we want to add a little title up to the top but we also want to learn how to pass information from one scene to another scene so right now if I go from scene to scene I go back I can't actually pass any information back and forth right now so this is where we left off we built this table view object but maybe what I want is I want to be able to select I don't know Rebecca Ferguson here and say you know view their detailed view or something like that where it give me more information about about our user I take it to a different scene so that's really the focus of this video is to is to give us the ability to pass data from one scene to another scene so let's get started in order to do that lets go into scene builder say let's let's first create another scene to go to so I'm going to right click here in my GUI demo and I say new empty effects them out and let's call it the person view and we'll use a Java controller finish so here's our our empty F XML file and our person view controller make a little bit bigger for you there we go okay so let's go into scene builder and build a really simple oh there's our person class sorry we don't need that let's go into the person view so make it the same size as our other screens go to layout I believe we were 800 by 600 and on here we'll just do something really basic let's put in a grid pane and we'll put some labels on it I'm just copying and pasting these labels everywhere ad row below okay this one's first name last name first day so this is sort of the same information available in the table view but we also have the ability to I figure out their age based on their birthday so we'll put that in and we'll add a button to go back so the back button hit save actually that's make each of these just a little bit bigger so they're more interesting Arielle mate okay so the fonts just a little bit bigger and we can put a little bit a little label up here in the middle and we'll say this is a the detail person make it massive area then move our grid down here actually why don't we make these things put these two things into a V box here yes excuse me my oh my all right and then me kidding me just a little narrower and then within RV box we can make everything centered and put some spacing between items a bit more aerial now we're good okay it's safe and then the other thing we need to do is we need to adjust our table view you need to add a button to this so here it Add button and let's call it a detailed first written review save so now we've got the gooeys what we have to do is we have to hook up these new scenes handle it so first things first let's look at our person view controller now we need to receive a person object right the person that's been selected from the table so what I'm going to do is I'm a private person and I thought the selected person so I'll have an instance variable that can hold the person object and I'm going to create a special method called a unit data so public void in it data and it will receive a person okay so this is I'll just say it's method except States person so we can Ischl eyes you still alive now Joel's going and typing the view so here we're going to need to label each of these the first name last name birthday and age so we can refer to them so I can say at F XML private label first name so just make it crispy label so we have the last name the birthday and the cage so if I pass in a person object right the first thing I'll do is I'll make them the selected person so I'll say selected person equals the person who's passed in and then I can update all these labels with their information so I could say first name label set the text to be the selected person and you take it the first name and I can send me with their last name and birthday how the birthdays going to be a little bit different because the birthday returns a local data object right so I can say I get birthday and then I'll say make that as string and then the age labeled except text be the selected person get their age do you know how that method classic rate okay we don't have a method called get age so but we want the person that's passed in we want to know their age so we can go to the let's go to our person class and here let's create a method called get age so we'll say public in get AIDS right so this is what this is where you start thinking about like objects so a person should know how old they are right so you can ask a person their age so here I'm just going to say return period between birthday local page now 50 years okay so what this does is given the person's birthday and whatever the current date is right so it's always always looks at the current date tell me how many years are between so now we go back to our controller here say this returns an integer so we have to convert that integer into a string so there's a few different ways of doing it we can just do integer dot parse int sorry parse to your string are you the person you need to convert so it's agreed have the person return the rage then turn their age into a string and then set the label accordingly save now in scene builder I have to actually I don't have to do anything in scene builder because we've already hooked up these labels so that's that's that alright now let's go into our table view controller so now here the table view we have to create a new method that will allow us to change over to so before we had to change scene button pushed okay now in this case we're going to do pretty much the exact same thing we're going to break it a little bit more so copy this paste it here and I'll say when this method is called it will pass the person object ask the selected person selected person object to the detail a view and so here let's call this I change to detail person view okay so here where it's going to be different is we submitted we're still going to use a parent but I can actually set up this loader as something that we can refer to more than once so we're going to do is I can say F XML loader loader equals new F XML loader and now that it's an object I can say loader set location oops set location and the location is going to be the same as this this is what's this is a long way of writing the same thing out here and then what I can do is I'll say loader time load ok so these three lines are equivalent to this one line here but there's a reason that I did it and all those breaks it out a little bit more than we're used to seeing what this allows us to do is we need to update our document so this one's called the person view after we've set the scene right so we've create the scene now we can we can access the controller and call a method so I can say person view controller okay so remember that's this class here person view controller and I'll just call this the controller equals loader dot get controller so what this does is because the this F XML loader we've we've set it to be looking at this file now I can get the controller for that file right so it will look in this file find out where the name of my controller is and pass it back in to this variable called controller and now I can use that can I consider the controller remember we created a method called initialized data and it passes in a person so you say in it data person now the thing is in order for this to work we need to have a person selected in a table so we're going to say table view and then we could say get selection model and then we can say selected item so what that does is it will return a person object okay so our table returns a person we pass that person to this method called in the data which is in our person view controller and that's how we get data in so let's save go into seeing builder here and hook up our button detailed person view so I've got a detailed person view so on action I want to change seems to the detailed person view safe all right hit the run button here go to table view select Rebecca click on the detailed person view oh and it blows up so what's going wrong here so we got a nullpointerexception in an it data person view controller line 30 so when we tried to set the text for first name label you got a null pointer exception so you there's a couple of reasons you can get those what it means is we've defined this first name label but we haven't necessarily initialized it so it's currently not so let's go into steam builder here if I click on my label look at my code ah I have not set these so we have to set them up first name label last name label birthday and lastly the age it saves so because I hadn't connected these up in our in our view when the controller tried to refer to it it was getting null so what was that let's try it again so Timmy to select Rebecca detail person view and it pulls us in as it to our detailed person view so you can see we're in a different scene here we've passed in the object of Rebecca Ferguson and that person object was able to calculate the age of 30 years now if it's like back it doesn't do anything yet because we haven't created that method yet so close that and in our person view controller now what we need to do is very similar to this we need to it's going to copy this exactly and here you go back to the table view I'm going to call this example of table view for XML I hit save try running it hope I haven't hooked up yet this won't work let's go in to put on my back button change screen button was pushed I'll save I'm not passing any information back try mr. t this time you kill person view it works I hit the back button and it comes back so there you have it very easy to set up objects to pass information into different scenes I hope you enjoyed the video I am going to upload it to github and you can always access my code from there
Info
Channel: Jaret Wright
Views: 57,448
Rating: undefined out of 5
Keywords: Javafx scene object MVC SceneBuilder
Id: Wc1a2KshJ4w
Channel Id: undefined
Length: 20min 31sec (1231 seconds)
Published: Wed Mar 29 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.