Adding and populating a TableView object using SceneBuilder

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back in our last video we found out how to change scenes so we added in this button so we click on it and we can go to a different scene and if we click on this button we can go back to the original scene and play around with our different you know three objects that we've been making excuse me so the goal here is in this this scene here is to add an actual table and this is sort of the idea of what we're going to do is we're going to add in this object here is called a table view object and inside of this object is actually reading instances of objects so here what I've done is I created a really simple class called person where they have a first name a last name and a birthday and all that we're going to do is we're going to create a few instances of people and we'll put it into the table in future videos we'll look at how we make the table editable how can we add new people into it how can we select people and delete them and do different things like that but for this video we're going to create a person class create into the people and populate this table so let's get started so first thing we're going to need to do is build a person class so I am just going to do and use your Java class and click person and it's going to be a very simple person obviously you can put anything you want in a table the secret is that you are doing a very simple one is just to keep it quick and unique so when you're using tables if you ever want to edit the contents and have it have it be a valuable thing you can't use regular strings you have you something called a simple string property which behaves a lot like a string but it also has listeners built into it that allow you to make some changes so and you're going to private and instead of string I'm going to say simple string property first name last name and we'll do private local date first and ID in our imports and then we'll just create a very simple constructor so I'm going to say insert code constructor and populate each of these items but what I'll pass in our string that's what most programs operate with and inside of my class what I'll do is I'll convert these trying to view is I'll say first name equals a new simple and then we can do this the most basic getting set to methods here so I'll say insert code getters and setters select them all and it will go today for us okay hit save and then if we go into our into our table view of XML document here so let's go into a scene builder and I'm going to go down in my control section so I find the table view and drag it on now by default it's going to give me two columns let's make these a little bit wider do all kinds of neat things like that what if I want more columns then what I have to do is I drag in another column and closed-form first name last name and okay you can make these any size you want hey shave now this doesn't actually allow us to populate this yet we're going to have to set that up inside of our controller but this does allow us to least be able to communicate back and forth with the with the objects so I'm gonna hit save did that go back to NetBeans so here in NetBeans I'm going to go to the controller for our example of a table view controller and at the class level I'm going to create some Christmas phrasal so we can configure the table and here's where I'm going to tell it that it's expecting a person object okay so at our influence then make sure you take the job fx1 not the java swing one we have to set up each of the columns independently so I get a parent classes person and what they're going to what it's going to give us is a string I think you call it the first Canadian column guide our imports again the last one here is to change the local aid object we'll call this the birthday call hit save and now we can go back in to feed builder if I click on the first name call here you see I can now say well that's the first name column that's the last name column and that's the birthday column and if I click on a table view itself okay that's a table view save maybe the background now whenever a genius is loaded the first thing that it does is is it calls a method called initialize okay so let's go down here there's a method called initialize want to initialize our schemes is where we would do it and so in here what we're going to do is have a first name call and we'll say sex cell value factory with a new property value and then we're going to tell it what to look for and these variable thing force called first name take linear property value don't worry the same thing for glassing and we're going to configure to look for blackmail information and then the last one which you know the I do they say the bumpers oh okay my apologies sir I would say I'm just well turned around there and then the last one will say the birthday column okay area so we've got that so now that allows it if I slow this and really know but here it's person at a local date said stuff like do Mary okay and then that sets up our column so it's a little and now the thing is here you want to load some load some data make a little method to do that for us so it's time you gate and if they tableview set the item all right create this guy goofy little method call get people and man created okay and the method here what it's going to do is it creates an observable list now you may remember I think I've mentioned this in previous videos but an observable list is a lot like an ArrayList behaves a lot like an array list but it's designed with some special features for for the GUI environment so because we're going to do is I'll set up an observable list first of all it's folded correctly nice h equals FX collection okay and then in here now we have this observable ArrayList okay people yeah just a new person and put in Frank of the mantra and use one and do the same thing just help others so I looked at these gates is actually actually really okay there we go so now we have Intel those people we just have to return it to ten people and this method will be happy let's run our program at this point go to our table view and so we didn't get quite what we wanted we've got the birthday is correct but it came back and said string property if you have these simple string objects being returned so all we have to do is go into our person class and let's let's update these slightly so what we need is public we need a way to get a string actually let's try I do a well it didn't like if you run it well now it doesn't like that okay nope definitely still didn't like it okay let's change them back to strings see alright and in the person class we're just gonna add in some methods that return strings instead of simple strings just get method what it does is it as I can play it sorry to find yes it's not reaching a string all right let's do this okay try that one more time sorry about the delay there but now we can see it returns the strings so we can see their names and their birthdays in a table and anything is the tables right out of the box they come with automatic sorting so we can go you know oldest to youngest or youngest to oldest to conserve our last names first names table views a really quick basement acting there you have it I'll upload this to github right now and then you can start playing around with it okay it looks like it's pushing let's just go check and make sure everything's good on github so again if you ever want to get these files you just go github slash chair right I'm going to click on repositories go to our GUI demo and you can see here it says add a table view objects so that was our last update and here we see the person file and the example of a table view controller click on it here's the actual code so looks like we're all good be in the next video when we start adding more functionality to our tables
Info
Channel: Jaret Wright
Views: 67,769
Rating: undefined out of 5
Keywords: Javafx TableView MVC SceneBuilder
Id: uh5R7D_vFto
Channel Id: undefined
Length: 18min 43sec (1123 seconds)
Published: Mon Mar 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.