Java GUI in Netbeans

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm gonna guide you on how to create a graphical user interface using the jframe form of NetBeans okay so in this application you'll see that I've created a blank application it's called GUI video and in my source packages I've gotten one package called GUI video and inside of it it's got my Java file that's containing my main method and I'm only gonna have basically one line of coding inside of this file so we're gonna create the wrist so I'm gonna create a new Java class first and this class is gonna be called a job so we're gonna have a job clause and that job Clause will then be used in an employee clause I'm just gonna quickly set up two classes that we're gonna use in this example so now the job Clause will have a private field let's say a private double and it's gonna call be called salary and then we're gonna have another private let's say string day and we're gonna have the name of the job so basically two fields and what I want to do here is to actually save these objects to a file but saving them as objects so I want to serialize them so for this you need to import Java dot IO dot star in order to get the serializable interface and we're gonna say implements serializable okay so name we've got the job class it's gonna have a salary and a name of job so now it's easy to just quickly can create by saying insert code I'm gonna create the constructor quickly I want to select both of them and that creates a new job for me I will also go and say let's give me the Gator and the satyrs day for both of them and say generate and then we've got the whole class so one method that I want to add here is the equals method so it's gonna be public boolean equals and it's gonna take in a job the same type of this Clause so I want to compare two objects of this class to make sure that there are this so in order to do this we can just use a simple return statement or let me just do the if statement and we can come back to this one so we're gonna basically taste if the object that you're busy creating or the object that's calling the method if this dot salary now the salary is a double so I can taste with a double equal sign there so this salary equals the job that was passed in salary and at the same time the it's a this dot name of job and now because this is a string I must use the equals method and an equals job dot name of job so if both fields of the one object is the same as both fields of the other object then we'll return true and then if it's not obviously the else pot they we will return false so now you can see they warned us in this if statement they it's redundant because we can actually remove all these return statements because that's the whole statement they and we will just say return at that whole statement so if this statement is true it's gonna return true if it's false it's going to return false okay so let's just take it into two two lines there so I'm testing if the salary of this object the salary is the same as a salary passed in and the name of jobs are the same then the two objects are the same and we return true okay so that's the job Clause so make sure you've got the job class they the second class I want to quickly create they is the employee Clause so we're gonna call this one employee we're gonna say finish and for the employee Clause we're gonna do the same thing I want to also have that import statement Java dot IO dot star and then in the employee Clause we're gonna have a few fields now we're gonna have private string we can have a name for an employee we're also gonna have a private string surname for the employee then we will have a job for the employee so we're gonna go to that job class and this one let's just call it job they that will be the job of the employee but you can see that the employee Clause then has a job so there's a hazard relationship between the employee and the job okay and then we're gonna have one last field a private int staff number and that will be the staff number of the employee okay so there's oh those are all the fields that we're gonna need they so we're gonna quickly create the construct today and I want to select all of them again and say generate and that is my constructor then let's get some Gators de and satyrs so the Gators and satyrs select all and then there's all your Gators and your satyrs alright so for now this is fine I'm not going to create copy constructors for job and so forth so in this case this this will suffice this will do this is a simple employee Clause and the main thing is that the employee claws are using a field that's part of another Clause okay so I'm gonna also say your implements serializable and this just means that we are able to save objects of these both these classes to a file but to a data file as an object as a whole okay so we've created the employee clause and we've created the job clause and we're gonna create or use those clauses now in order to create objects in this graphical user interface of ours okay so let's create the first graphical user interface page that we're gonna do now so this is a bit new we're gonna go to new and instead of saying Java cloths now I'm gonna say jframe form so if you open up jframe form it it asks you for the class name so you can see it's also connected to a clause and I want to call this one let's say add new job so this clause or this form will help me to add a new job and you can see that they they give us then I think I'm gonna close down services tab they said we can have a bit more space although it doesn't give us more space okay so this is the add new job so you can see that this is now the window I can resize this window the size I want it I can resize it horizontally vertically or by just dragging to the corner Vain's resizing it so it's up to you how you want to resize it and you can see at the bottom at the right hand side corner you can see the width and the height of that specific frame you can also use that eye there to give you a preview of how big it will basically display okay so now on this screen I want to go and create or add a few components to the screen so you see that there's a pellet that was opened up and the main thing here you can see that we've got swing con containers we've got swing control swing menus and so forth and there's also a WT that you can use but remember that we would rather use the swing components so for the swing components let's go to controls I'm gonna add a new label India so there's the label I just drag the label click and drag the label now this label if I double click on it will be create a new job by entering let's say the data below right so there's a basically just the label that we dragged in there and for that label there's a few properties now that you can see it so on the right hand side you can see there's properties by clicking on that label you can say it's some property so I'm gonna set the font they to be of size 18 and bold and say ok and you can then see that that changes our label to look a bit differently now so I'm gonna read just recent of it in the screen there if I click on the eye you can see that's basically how it's gonna look like okay so then the next item that I'm gonna add there is a label again and we're gonna ask the user to please give us the name of the job okay so there's the name of the job and then we're gonna ask him to enter that in a text field so under swing controls make sure you go to text field now look at my alignment lines they there's a space of let's say t-they and you can see there's a bottom line that's a line that they with the text so the text of the text field as well as the text of the label will be aligned okay so there's the text field on the text field you can see it is editable I'm gonna go too and just move this one up a bit you can see there's the text of the text field so I'm gonna remove the text of the text field day and maybe on that text field let's just make it a bit bigger something like this okay so that's the name of the job then let's add another label this I'm gonna drag in another label then now look at the lines again to actually align them so I'm gonna say let's add two spaces there but can you see it's aligned on the right hand side okay so I'm gonna say for this label I'm gonna say salary for this job and you can now see there are nicely aligned even if you click on the I they they are nicely aligned okay so then we're gonna have also a text field thing so let's drag in that text field I want to have it aligned on the side for that one as well and just drag it until you get the alignment lines they so you'll see they they are basically aligned okay so now the tick's inside of that one also if I can make this one a bit bigger the text I'm gonna remove it so it will just look like that then I'm gonna add a button there at the bottom to actually save this new job so I'm gonna have a button they dragged in the button there's the button you can decide where you want to place that button this will open up this one a bit better so let's just have a look at the idea I think we can actually make this window a bit smaller so let's just resize this window to something like let's say this so if you look at it this is how it will pop up to the screen into the job salary for the job and then we're gonna have that button D so the button if you click on it remember these properties to sit for the button as well so for the button I'm gonna set the text on the button to save and that will change to save thee now this looks a bit boring so let's say we want to add a picture to the Save button so what I want you to do is to go to HTTP tinyurl.com in GUI pics and by clicking on that or entering on that one it will take you to a web page where it start automatically downloading the file for you to use and that file is basically just the folder called pics and it looks like this there's a few pictures that we're going to use so you can see there's a safe picture so let's go back to to NetBeans and I'm gonna click on save there and I want to import that save picture now to my project so you can see that I can go down there's an icon property if you click on the button there's an icon property and I'm gonna click there on the dots the three dots on the right-hand side of it and say I want to import to my project so now in my let me just go to my desktop there and there's my pics folder they that I've asked you to basically download and there's the safe picture so I'm gonna say next there and I'm gonna say finish and you can see there's the picture and I'm gonna say okay thee so you can see now I've got a nice picture to my button called save right so that's basically it then fall for this window if you click on it you can see it looks like this so we're gonna enter the name of the job let's say cleaner and the salary 10,000 or whatever if we click on save we will add a new object to our database at this stage we haven't done any coding for the save buttons and nothing will work so your first kind of design the the graphical user interface and then come back to the coding okay so the next window that I want to create I'm gonna close down that one let's just save it first I'm gonna go to my project folder on my package again they and I'm gonna say jframe form again but this form will now be add employee so now if we have jobs we can start adding employees okay let's see if I can resize this a bit okay I'm just gonna make it a bit bigger they so what we want for this one too adding a new employee we also have a label right at the top that will tell us what we're basically doing me and I'm gonna say add a new employee by entering the details below okay so let's change the font for this one as well so I'm gonna go to the property font I want to have it bold and 18 the same as the previous window so there's the window it's gonna show a Danny on employee by entering the details below okay so let's see if we can make this one just a bit bigger they something like this okay so we'll see how we can get past this small windows now in this video but in any case the first thing I want to add then is a label and that label will say inter employee name so remember in our employee class we asked for the name of the employee the surname so let's add another label they align it to the right there and we're gonna say inter employee surname and then we'll have another label they we were gonna ask inter or let's say choose a job and at the job we will choose now from a list of jobs that we created early on so we're gonna use a combo box for that one and then for the last one that we're going to ask the user day to enter is the stop number enter the stuff number right so they've got all the labels that were asking the questions now for the employees name we can use a text field so let's make it a two space day but aligned with the text the same with this text field aligned with that one and then we're gonna use a combo box is the combo box so we're gonna drag in the combo box they also aligned to those text fields and then the last one will also be a text field again aligned to the rest right so they've got if we open it up it looks something like this now it's not very beautiful so let's just make it a lot bigger make those fields a lot bigger so we've got some space for the user to type and we're gonna have a bit more data in that combo box so let's make everything the same size you can make it smaller if you want so basically then it will look something like this so there will be our items that we're gonna choose from which will be our jobs at this stage just a few items that was added they if you're only using a combo box like it's here you can actually go to the combo box let's open up the properties a bit and you can see the model they by opening it up you can just basically randomly type your items that you want to have thee so in this case I'm gonna keep it as item 1 2 3 & 4 because we're gonna change them in coding later on right so the last component I'm gonna use here again is at the button I'm gonna add the button to the screen and the text on that button again will be Save and the icon for the button now because we added the the Save button to our project it will be in this list of icons so I'm gonna choose save they and I've got the exact same Save button thee so if I click on the preview now it looks something like this maybe we can move the button up a bit let's say today and we make this frame bit smaller let's look at this one I think this would be great so let's just remove the text in those text fields so if you if you select your text fields I'm gonna select them all by holding down shift select all the text fields and let me just see if I can get the text they basically just remove the text property they and you'll see that all of them is now empty so the user will type his name the surname you'll choose the job from the list and then he'll entice staff number and then click on save right so we've done with that frame as well we can save close down that frame now the next frame that we're going to do they will be a new jframe form again and I'm gonna call this form edit employee and we're gonna say finish there now I'm not going to do every edit function of deleting and everything like that because you can go and have a look at how you do that I think if your grass becomes concept of just changing a value then it's easy to delete and update to whatever you want to do with it so for this frame let's go and add a label at the top and this label will basically say edit employee data then let's go to the font and the font will again be bold and 18 like the others and this one will just say edit employee data I think we're gonna have this one also a bit bigger see if it can resize it a bit bigger there and get it in the middle somewhere that would be fine okay so then we're gonna have a label again and the label will they be choose the employee and then after that label we're gonna have the employees name so let's say two spaces in line to the right hand side double click there and it's going to be employee name then another one they two spaces line to the right let's say employee surname and then another label they double click on it employee job and then the last label they will be for the staff number right so we're gonna double click there and say employee staff number right so there's the labels that we're gonna have so if you run it or place if you just click on that I icon thee you can see they are nicely aligned to the right hand side and there's enough space between them ok so now we add some components they in the first one we're gonna add their four to choose the employee will be a combo box so we'll go to the file where all our employees are saved and we will get our employee data from that file and show them all inside of this combo box then the employees name will just be a normal text field just try that again okay there we go right just resize it the same size as that one that's the employees name then the surname the exact same thing line it to the left hand side there and to the right they are the same size then also for the employees job we're gonna use a combo box again to show the job they will show all the jobs so that the user can change the Dadaab the job there if needed but also show the specific users job and then the staff number will be a normal text field again so let's align them to the right and there we go let's click on the I quickie it will look like this and I think that would be fine let's just add a button at the bottom so that if you are done saving the data we will have that button at the bottom okay for that button again let's just have that picture day so the icon property will be save and the text property for that button will be save as well okay so then we have a button that looks like this let's just save a look at it I think maybe let's move the button up a small bit so yes I think this would be fine the user will choose the employee it will show his name username his job and his staff number and then you were able to make changes and click on save let's just quickly remove for all three those text fields let's just remove the text as this one this one and that one holding down the shift key if you go to the text property you are basically emptying the text so clicking on the eye again will give you this layout and this is the layout that we want so I think for this example is added delete button there as well next to the save let's move it a bit to the left we're gonna add another button there next to it and we're gonna have that button let's say the text a will be delete and then we'll check quickly for the icon again so because we only have the Save icon they let's click on the three dots again then go to import to your project and one of the pictures I gave you was that exit button there so I'm gonna use that one and say finish and it looks like this and we're gonna say okay so there's there's your delete button then maybe we can resize this button to be the same size as the other one so it will basically look like this let's just see how it looks okay so we've got save and we've got delete right so we're gonna be able to save new employee data or change the data and we can we are also able to delete this specific user right so that's the Edit employee data and then the last one that I'm gonna do now quickly is actual front page of our application which will have a simple menu system so let's create that one also I'm gonna say new jframe form and let's call this one the menu and then say finish so this will be my main window that will show right at the start and if we go then to our main method they we can basically just say new menu and let's just call dot set visible and we set the visibility to true for that specific component so if you run the application now you'll see that that window pops up but we haven't done anything yet okay so this this is the menu window now and this menu window I want to add a few properties they or few things to this one so I think the first thing that we need to do this you close down swing controls and you're gonna go to swing menus now so the first thing we'll add there is the menu bar now the menu bar will give you two fault or two menus already the file menu and the Edit menu so we're going to keep the fall menu but the Edit menu you're gonna right click and say edit the tics they we're gonna have them in the menu called add and then I want to have another menu so I'm gonna drag in another menu they and that menu right-clicking on it or just clicking it twice not double clicking it by clicking it twice or just saying edit the text and we're gonna have that one as edit now in every menu that we've got they we want to add some values to them so for the file menu for example we want seven menu-item days I'm gonna drag in a menu item and I'm gonna say if you double-click on it you'll see it takes you inside of your coding which I don't want to so you can see on any form this source and there's design so design takes you back to the visual editor and source takes you back to the coding so let's go back to design and right click and say edit text and then we're gonna have this one as exit so that the user can exit the application by using file exit so on the exit they on the exit menu item we're gonna go and change some values there as well let's set the icon day to that same exit picture so you can see then in the file menu we've got exit day now let's go to the file menu itself so if you click on file then you go to icon and we're gonna go to the three dots again an import theme and we're gonna use the folder one so finish at that so you can see then if you look at okay this preview button actually doesn't show your menu system so for that to actually see something you need to run your application and then by running the application you can see this my file and exit okay so now let's go to add quickly and for add let's use a picture again and we're gonna go to add they say next so finish and that will be our add picture right and then for edit let's go also to icon three dots import to the project go to edit next finish and that's the edit picture we're gonna say okay so we'll have if we run it now we'll have our application with those three menus we've got file and exit add and edit and now we're gonna add some values or some menu items to those as well so for your add we're gonna add two menu items they so one two menu items on the first one let's edit the text a that one will be add new employee maybe we'll have the new they also a capital in and then for this one will also edit the text and this one will be add new job right now for for add new employee let's consider an icon there's wall so we're gonna go to import add employees the pictures name finish and it's gonna look like that add new job go to the icon again three dots and let's take job they next finish and it will look something like that right so we've got our file system now and then the last one day is to edit a new employee or edit on a new one edit existing employee so we're gonna add in a menu item and we're gonna say edit employee and then if you click on that one let's use that same icon that's called edit so you can just choose it from the list if it's already there and then let's look at our application by running it quickly so there we go the sustain the file menu the add menu and the edit menu right easy to do a menu system now the last thing I want to do quickly is to go to the content pane and I think let's add a label here in the content pane so let's go back to swing controls I'm gonna add a label they but this label will have no ticks but we're gonna have an icon set to it so it's import the project we're gonna use that main day so finish and okay you see it's a big picture and I'm gonna remove the text on the label right they can just see if I can resize something here that I can see what's going on right almost day right so this is basically the picture it's gonna show like that let's just look at it that's the window so maybe we can just move that picture a bit or maybe make this window a bit smaller if it's possible there we go look at it now okay maybe we can just move it a bit to the right and let's look at it now so your window will basically look like this let's run it quickly and see how it runs and there we go we've got a nice picture day we've got the file the ad and the Edit menus and now we can actually start Cody now for every one of my windows you can see that I don't have that the text sit as the title there at the top so there's a few things that we want to do now so inside of my my frame so if you click on your frame itself not on the on the label they just see if I changed something now still looks fine so if you click on your content pane and another way of doing it is to go to your jframe Day at the top if you can see and in the navigator they just click on your jframe so on your jframe you can see that there's a default close operation so on the very first page we want to have the default close operation as exit on clothes and the title will be something like welcome to the employee system that's the title of your window so now if we click on save and we run this again you will see the title of your window will now have that specific title although it's not showing you just see it's the title let's save again look at the ID clicking on the eye shows it so I think it was just not saved it let's see if it runs now ok so there we have it welcome to the employee system at the top that's your your title for the window and if I click there it my window so let's just quickly have a look at all the other windows that we defined so I'm gonna close down menu now let's go to add employee quickly and then you can see there is also default close operation but now if we add a new employee in we close that window we don't want the whole application to close so we're just gonna say dispose which removes the window but it's not actually closing down my application so we can just have this one add new employee and then we can save then let's close down that one let's go to add new job and clicking on the jframe d default close operation also disposed and the title will be create new job I just want to make sure it's not clicked anymore save it so that one should be fine let's look at this one create new job yes and then let's say edit employee make sure you on the jframe default close operation dispose edit employee details right so let's save it and if you click on that one it should say edit employee details ok so if we run the application now well basically we haven't set up anything so we'll just see the main screen and then if you click on it it will close down ok so now the the next thing that we want to do quickly is to sit what will happen if we then click on every item in the menu so I'm gonna go to the menu system quickly so this is the menu one I'm gonna just resize this a bit let's just make it a bit bigger so let's say we click on the exit button d so by getting to get to the the private in a class that activates this exit button you need to double click and you can see if you double click it takes you directly do that inner classes actionperformed method and you can say what did she do so we're gonna say system dot exit and we're just going to pass in the 0 they which will then exit your system so we'll run it now and the user goes to the file menu there's the follow menu and clicks on exit you can see that the whole application exits okay so let's go back to design then now for the add method or sorry to the add menu and you're gonna click on add new employee just double click that one it will also take you if you go but up you can see there's the cursor it takes you to that specific buttons actionperformed method so what we want to do here is to start remember we are now at adding a new employee so in order to add a new employee we're gonna call that clause and that Clause is a new add employee if I remember correctly on the left hand side that's called add employees so we're gonna say new add employee and we're gonna set it to is no set visible and we're gonna set the visibility to true and that will start showing it so let's just see if it runs if you run it now and you go to add new employee oh there it is at the back don't know why this windows in the front will change the settings now but you can see there's there's the employee or the add new employee they're just level look at the settings for this jframe quickly the menu jframe so we go to the menu now let's just go to design and look at the properties quickly for the jframe you see it's always on top so I'm gonna remove the always on top they so it should not be always on top so if we run it now again let's go to add add new employee and there we can start adding the new employee data you can see if I close it the whole application doesn't close but closing that one closes down the whole application okay so now the next one will be in the add menu the add new job so for the job now you can see there is my cursor so for the add new job the class is called add new job so we could just say new add new job that creates a new instance of it and then on that instance we're gonna sit set the visibility to true then go back to design the last one day is then for edit employees if you double click that one it also takes you to that private in a clause and what we'll do they is to call the Edit employee we'll go back to source edit employee that's the name of the class also called edit employee let's just add that one also so it's gonna be new edit employee employee dot set visible and we're gonna set the visibility to true all right so let's save and run this and now we should be able to to work between those windows so if I say file exited exits the application if I run again if I go to add new employee I'm getting the add new employee box there if I go to add new job it's opening the add new job if I go to edit employee it opens up edit employee ok so that's how you'll move or use your menu system in order to do what we want to do so basically we are done with this frame where we choose what to do so I'm gonna close it down also close down that frame and also my my Gweedore job are they and then what we're gonna do now is to go to add new job so we will start with this one so add new job looks like this we can ask the user to enter his in the name of the job and the salary of the job and then he's gonna click on save so the Save button is the one that we are interested in you can see that this one is called jtextfield one that one is called jtextfield two so you can refer to them using those or you can right-click on the condor specific component and say change the variables name so if you don't want to have a jtextfield one you can change it to let's say job name and you can see the name of the variable has now changed okay so let's just do the same for the stick so I'm not gonna do it to all of the text fields so I'm gonna change this variable name to say salary job and say okay so it's a job name or maybe say sorry job salary then it's named the same way okay so job name and job salaries then the two text fields there and then if we click on save we want to save the data of those two so if you click on the Save button now let's just go up and see where's that so you can see there's the coding for what should happen when you click on the Save button so the first thing that we need to do is to get whatever they user type they in those two text fields so it's job name and chopped celery so we can say string name oh yeah let's say job name see now the variables will give us a problem this I called this one job name and job salary so maybe let's just call it a name and then the name will be going to that text field which will be job name and I'm gonna say job name dot get text and remember if the user is typing the text it could add some white space so if you remember the trim method that will help you also to remove some white space that they use ain't it and then the next one we can get there is the job what is the job or the name of that job sorry that's not the name of the job that is the salary so the salary will be then what did we call that one salary for the job will be chopped celery that's the name of the Edit or the text field so it's job salary dot get text gets the text whatever the user typed and then you can use the trim method again but just to make sure that the usually user actually types something before clicking on the Save button because let's say it doesn't click anything they and then he clicks the Save button then you're gonna have a problem saving those data because there's nothing in it so the first thing we're gonna do is the first test if the first jtextfield so which is job name it's a job name come on dot get text and then we're going to call the ease empty method in empty is empty method to check whether that whatever the user typed a is actually empty so basically it didn't type anything so if that one is empty or let's say the other one is what job salary job salary dot get text dot is empty so if either one of the two is empty then we want to just show a normal job in painter to use job champagne I'm just gonna import Java X dot swing dot star and let's go down and say right there we want to show a message J option pain dot show message dialog and the message that I want to show to the user is something like please enter all fields right so let's just taste it quickly you'll see what happens now actually I think it's gonna throw me an exception now let's go to add new job they and say save you can see please into all fields okay so it went fine let's just close it down and then the else part they the out spot is if there is then something entered we can get the values by calling this I'm going to cut it and I'm gonna baste at thee now the main thing here that that I want to use to save the data so I'm just gonna save this file and we're going to continue with this one I want to create a method first we're gonna get some data from files which we still doesn't have but we'll we'll set it up now so we're gonna have a method public void populate ArrayList and this method will help me to read the data from the file first and before we add new jobs so we want to first see where's the existing jobs and then to the existing jobs we're gonna add this new job so in order for this to work I also need an import the Java dot util dot star which will help me to use the ArrayList so at the top I'm gonna create we can use a private one yeah let's just use a normal public ArrayList a so it cannot be ArrayList and the type of this ArrayList will be job so we're gonna have an ArrayList full of job objects and let's call it jobs now inside of the inert components they just off the inert component so this initialize component basically bolts the coding for your user interface that you design and if you go down a bit you can see there's generated code the-- if i open that up but this is the generated code for that user interface that we just both okay so just after that we'll initialize the ArrayList so we're going to say jobs equals new ArrayList and also of type job obviously okay so they the ArrayList has been initially initialized and then i want to call this one to say populate the ArrayList for me and that just means that i want to get data added to the jobs array ArrayList so in this case what we want to do is to go to a specific file and get the content of that file so i'm gonna have a try and a catch block here and the sketch will just catch the i/o exception you remember in previous videos we said at the top of the method they should be throws ioexception so now instead of saying throws I exception we catch the i/o exception they okay so in order for the i/o exception to work we also need to have an import statement a Java dot IO dot star right and then it should be fine okay so in the catch there we can just show a chirp champagne dot show message dialog and then null for the first document null second argument what you want to show to the user so we're just going to go with that II object dot gape message and that gets me the exception message displayed to the user now it's it's still giving us a problem there because we haven't done anything in the try block that could throw an i/o exception so let's see what we need to do the first thing I'm gonna do is to open up that false I'm gonna use the file input stream clause and we're gonna call it let's say file equals new file input stream that's working with data files and let's say the name of the file will be jobs dot txt no dot dat a data file and then to make a connection to that file to actually start reading from it we're gonna read object so it's gonna be an object input stream and let's call this one input file equals new object input stream and you're gonna pass in your faul object a so that will help us to actually now read from the file so this one makes the connection to the file this one has got all the methods that we can read from the file then if you remember correctly we're gonna use a boolean variable to check whether it's the end of the fall and we're gonna set that to false right at the start because if you start reading it is not the end of the file and then we use a while loop that says not end of all while it's not the end of the fall we'll keep on extracting data right so in this while loop we also have a try/catch to basically find out when are we at the end of the file so we can acai we're gonna say catch they're the end of file exception it's called a key as well and then we're gonna make sure that we catch any other exception as well so I'm gonna have the exception let's say FV so to catch any other type of exception that could okay right so the end of file exception let's just share jokes and playing there again I think I'm gonna copy this one let's copy that line day and in this sketch as well as in that catch we're gonna have the same thing but this one will just be the FV so that object and the e dot get message now in the try so what happens if now if the end of fall happens we're not gonna show a message we will actually change this end of file object or sorry boolean variable to true okay so when we when there's an end of file exception we know that we are at the end of the file and we cannot read anymore so in the try will actually try reading from that file so while reading we're gonna add this to our ArrayList that has been declared at the top so there's your rail is defined and it's declared initialized and then we're gonna say jobs dot add and now we want to add an object from the file while you can still read from it so we're gonna say add and the method that you're gonna use is in the input file so we call this one input file that's going to be input file dot read object and that'n reads a new object but that object that gets returned is of type object and you need to cost it to the type that you saved it as and we're gonna save them as job objects so by calling input file that read object that gives me the job object that was saved into the file and converting that to make sure that your job object and we're adding that to the ArrayList called jobs okay so we can save now so this is basically what's going to happen and then after your while loop we're just gonna close that file so we're gonna say input file and that releases the resources for that file and it saves and closes the file okay so what happened now in this method populate ArrayList so it will go to the jobs dot dat file which we will not have right at the start so you'll see if we run this now it's gonna actually actually throws an exception but we catch that exception so it won't hold our application so we say add new job not found but it still shows us the window so we can add a new one now and create the file okay so no problem if the file does not exist we're just going to show the user the file does not exist it basically basically catches this i/o exception and it shows the message as the file does not exist okay so that doesn't halt our application which is what we want okay so now in the let's go back to design so now we have all the previously saved job objects so if we click on save now what did we do we checked if well if those two fields are empty or not if they are empty we will show please enter all fields otherwise we get the name we get the salary and now we can create a new job object so we can say job job equals new job and then the job could be let me just go to the job clause quickly we're going to accept the salary first and then the name of the job so first the salary but now the salary should be an integer or a double I think they can just see it should be a double so let's convert that to a double so it's going to be double dot pause double and we're gonna pass in that string salary day which will convert that salary to a double and the second argument will be then the name of the job so there is our new job object created and what we want to do now is to go to the jobs ArrayList and we want to add this job to the job ArrayList right and that is it now we're saving that new job okay so now after this we want to make sure that we call a method to save all the jobs back to the file so I think let's create a method after this one and we're gonna call this one public let's make it void as well and we're gonna say save jobs to file let's open it up there you're gonna save all the jobs to the file now and what we want to do is to use a try-catch block thee so inside of this we're gonna have a try-catch block in order to save the contents now to a file and that will also be an IO exception that we're gonna catch there and that are exceptionally can also just show that J option pain D by saying e get message right so now if we want to save those jobs now to the file we need to have that file output stream object now so you remember when we read it we use for input streams to write it's fall upward stream and then let's call this one also fall equals new file output stream and now passing into this one will be that same file that we read from which is job's dot dat so we're gonna save back to that file again and then we're gonna say object output stream let's call this one out file output file equals new object output stream and we're gonna pass in that file object ok so now we made a connection to the file again and we are now ready to start writing to the file so what we basically want to write to that file is in the jobs ArrayList so we're gonna run through that ArrayList by saying integer I equals 0 I less then and it's gonna be jobs dot size which gives me the size of the ArrayList and then you're gonna say I plus plus so now we're gonna write that jobs ArrayList every single job object inside of it we will write to the jobs dot da DAT file so we're just gonna say output file dot and the method you're going to use is write object and the object that you want to write is going to the jobs ArrayList dot get and you're gonna go to get IV now what is this gonna do again jobs is the ArrayList get I gets a job object and we're saving that object to the file called jobs dot dat and then after saving you can just go and say output file output file the close that closes the file and just remember to also tell the user that you actually saved this thing or this job successfully so let's just use the jobs and plane again but instead of saying you don't get message day we will say let's say successfully saved and then if you want to close down this window after saving you can just say this dot dispose and that will close down the window for you or the frame for you right so basically now after calling saves save jobs to file it will create that file for us so let's run a run this thing quickly you'll see the very first time we run it add a new job it will say that there's no such file or directory so I'm gonna add a new job let's say cleaner and the salary for this job is 5000 grand and I'm gonna save so let's just see what's wrong with this button now that's not doing anything it's close it down see if there was any exception that occurred no exception so let's just see ah we haven't we haven't called this method so save jobs to fall is the method and we need to call it some way so obviously when we click on the Save button let's just double click the button after adding that job to the ArrayList we're gonna call that save controlspace save jobs to file that saves the jobs all the jobs in the array to the fall and it displays the message to the user lets us run it quickly so we're gonna go to add new job you can see that it's still empty it's cleaner again the salary 5,000 so save and you can see successfully saved and it closes the window when we're done so the next time I go to add employee now you can see no error about the file that's not found because that file has now been created okay so that's how we add a new job so now we need to go and have a look at how can we add a new employee and that's basically the same thing so I'm gonna keep up open this file and let's go to add employee quickly now for this window you can see that what we need from the users is his name his surname and we need the jobs as well so again we need to read from a file to get all the jobs but also to get all the employees that's currently there so that we can add the simply to the other employees in the file ok so let's go to the source code quickly before we do anything else and I'm gonna add an import statement at the top so we're gonna say import but it's Li the same import statements that we had there so let's just copy them in the add new job it will be all of those and then we need to go and create that ArrayList so I'm gonna say ArrayList or both array lists one will be off type job and we're going to call it jobs again and the other ArrayList will be off type employee that's the other class we created and we're gonna have this one named employees now let's set our both of them quickly so after inert components we're gonna have jobs equals a new ArrayList and the ArrayList will be off type job and let's just close it down and in the employees equals new ArrayList and that one will be employee as well okay so that is sitting up the two arraylists and now before we do anything else we want to get all the existing jobs and employees from the files so I'm gonna have that same method again that's called populate ArrayList well do too much they they just go down a bit I want to take the whole populate ArrayList method they that one gonna copy it and go back and I'm gonna add it just after my constructively for the jframe and there's populate ArrayList so you agree then that this populated ray list will get all of my jobs that's saved inside of the job dot dat so I'm gonna do the exact same thing again so I'm gonna copy that try-catch block thee copy it and then just after that trycatch I'm gonna do the exact same thing again but now instead of reading from job CIT I'm gonna have employees dot di T and let's call this one we can keep it as fall let's make it fall to input fall to fall to input fall to and I think we can say the rest is fine input fall to close okay just to make sure that it's it's not giving us problems later on okay so they we connect to the employees dot data file again the exact same thing but instead of adding two jobs now we're gonna say employees dot add and now you can see it's got a problem there because that should be now off tight employee when we're reading it back okay so this one will then get all the data from the job store data file and add it to the jobs ArrayList and the next try-catch block will get every data from the employees dot data file and add it to the employees ArrayList right so now we can save so what we've got now at this stage when this frame runs right at the start we can make a connection to the files and we get all the employees and we get all the jobs so now when the user goes and he unsaved after entering everything remember before he actually sees this window we want to actually show him all of the jobs inside of this specific combo box so now a nice thing about this source code that's been generated for you let's just go down until you get to generated code so you'll see some way they there's your jcombobox one and it's got a stud SiC model they on how they set the data to that combo box so I'm gonna copy that line basically stealing that line of code they to see how can we sit some new data to that specific combo box so I'm gonna copy that whole line day so if you missed that now it's under generated code if you scroll a bit down and that's your inert components method and inside of it there's this one line that sets the model to your combo box because I'm gonna use that line and if you have a look at populate ArrayList in let's say inside of the constructor within call populate ArrayList so that gets everything from the files running this method and then after that I'm gonna set that combo box now so in order to set the combo box I need to have string values for that jobs so I'm gonna say for integer I equals 0 I less then actually we can do this you can do that in a for loop right here but you can also do it while you are basically getting the data Y and adding them to the jobs array ArrayList so I think this rather just do it there at the top so we don't confuse anyone so what I'm gonna need there is a string array so you can see this let me just paste that line of code that we stole there from the init components you can see that this one line jcombobox that sick model that's got a problem here now expect it as okay now it's it's it's because I'm not done with this line day so let me just remove it quickly okay so then this set mod fine so you'll see basically that the sit model takes in a new string array so that's why I want to declare a string array here and let's call this one jobs array and I'm gonna sit this array the same size as there's elements in my jobs array now so I'm gonna say jobs dot size and that gives me the array to be exactly the same size as the jobs array they okay so now all the same size of the jobs ArrayList now I'm gonna run a for loop integer I equals 0 I less then it's a job start size gives me the number of elements inside of the jobs ArrayList which is the number of jobs I've got saved on the file and I plus plus and then I'm gonna do the following now inside of this for loop I want to run through every element in the jobs ArrayList and create the string representing it so I'm gonna say jobs array which is the array now at that position of I and I'm gonna set it to jobs dot get I that gives me the object from the ArrayList and I'm gonna call gate name of job and then maybe we can add a plus there and put a comma and then also show the amount of rent or the rent value for that specific job by calling jobs dot gate I again gets the object dot get salary will get you the salary and I think let's use a decimal format Clause also so it's gonna be import Java dot text dot star to get the decimal format a clause I think lets me do click just declare it at the top decimal format format and then somewhere year let's do it they format equals new decimal format and then the decimal format or should format the code let's put hash comma hash hash hash dot zero zero so we can separate the thousands with a comedy okay so then when we say jobs dot get salary I will say format dot format and then passing in that whole jobs dot get salary and that will format it nicely so in the for loop then when it when it runs we're gonna get the jobs or set the jobs array at this every single position in the loop by going to the jobs ArrayList and getting the name and the salary and setting them and then when this for loop is done you'll actually you can we can call jcombobox one dot set model but instead of having this initialized string there we're gonna have our own one that we called jobs array and that sets the model so let's just let's just see if it works I've been run it now let's go to add a new employee in please know nothing exists but you can see it got the job from this cleaner job from our jobs data file that already exists so if I close down this one and I go and add a new job let's add a program at here and let's say the salary there is 25,000 we save it successfully saved and we go to add new employee there's no employees data but you can see that job has been added to this combo box ok so now we can do the rest we can then ask the user for the employee for his name and his surname and his staff number and get those values so you will need to choose a job day but we don't need to check if it's empty because it will always have some value inside of it ok so now let's go and look at the data that we want to get from this so you can see that takes fields jtextfield one for the name the surname and so basically today jtextfield one two and three so let's go to the Save button now so double-click your save button it takes you to the private in a clause to the actionperformed method and you can start to your coding days so the first thing we want to do again is to taste if those text fields are empty or not so it's gonna be jtextfield one dot K text dot is empty so that one is empty o jtextfield to get text dot is empty oh let me just copy this one o day jtextfield three could also be empty so if any one of them are empty we're gonna show a J option pain dot show message dialog first argument no second argument please enter all fields right and then if all the fields are entered that is now the else birthday if you know everything is entered the else part will be what we want to do now if the if everything is entered so the first thing if you go to your design again you want to have the name and the surname so let's get the name of the surname first so we're gonna say string name equals J text field that's the first text field dot K text and you can call dot trim there on it to make sure that there's no white space then the surname will be jtextfield one sorry two dot K text dot trim that gets me the surname so if you go back to design that's jtextfield one this one click this jtextfield to then help to choose a job they you need to go to to get either the selected item or you can go and get the selected index so I think the index will be big to you to use so to choose a job they let's say int job index equals now look at the combo box quickly if you click on design the combo box is jcombobox from one so go back to source jcombobox one dot get selected index now that gets me the selected index the one that the users currently selected now remember the index in your combo box will be the exact same index that you have in your jobs ArrayList because right at the top we said how did we create that combo box we said we use the using this jobs array which is the same index as jobs so basically we can just go and say if that is the index then I know the job will be or the job object it's called job job equals going to the jobs ArrayList gate and which positions should I get it at that job index so now I know that that is the jobs object that the guy selected which is part of my ArrayList so now I've got the name I've got the surname I've got the job so the last thing I need then I've got the name surname and the job the last one I need is the staff number so to get the staff numbers also I think the star staff numbers in fact an integer so let's just call that one stuff just have a look at this employee class quickly the staff number is an integer yes so let's just go and say stop number they equals now we need to cause this to an integer because what you get from a text field is always a string so integer integer dot pause int and then we're gonna pass in what we want to convert an integer which is jtextfield three now if you go back to design is this one jtextfield three dot get text dot trim just to make sure that there is no white space and then that will give us the staff number as an integer and now I can create the new employee object so employee employee equals new employee and now let's look at the employee loss quickly you can see the construct exits the name the surname the job and the staff number so it's first the name the industry name then the job object and then the staff number and they our object has been created right so now we've got a new employee that we created and now what we need to do is to go and add this employee to our ArrayList so our ArrayList is called employees dot add and we're gonna add this new employee right and that is it for saving the employee but now remember it's an aside in the ArrayList we need to go and save it if you remember add new job we had a method save the jobs to file so I'm gonna copy that method quickly let's go back to add employee and we're gonna have that exact same method so I think let's add a method before the populate ArrayList a doesn't matter really where you add it but now to save the jobs it's not gonna be jobs it's going to be saved employees to file we need to go to the employees employees dot data file you can see it's the output stream so you want to save it what do we want to save everything in my employees ArrayList so it must be employees ArrayList and getting all the objects and saving them to the employees dot data file what at the populate ArrayList method - it got all the jobs placed them in the jobs ArrayList it got all the employees and placed it inside of the employees ArrayList so now when we call this method save employees to file employees already exist with current employees in it or maybe nothing and it's it's saving all the data inside of your employees ArrayList back to the fall so save employees to fall is the method we want to call in right after doing that save employees to file and that saves my data to the file now also remember let's just go back to that method save and please to file successfully saved ok no no that's that's finding ok so if you run it now and we go and say add new employee there's no employees yet so let's add it an employee they let's say John Rambo he's a programmer and his staff numbers 1 2 3 4 5 click on save you can see it successfully saved we dispose of it and when we go back to add new employee you can see it exists now it doesn't ask us for the Fall or say that the file does not exist anymore so unfortunately we can't see that employee right now we'll do that in the next window ok so now we are able to save a new employee and we can check that by going to edit the employee data which we'll do now ok so let's go to editing the employee data now so I think I'm gonna have open add employee close that one closed job and close employee and then let's go to edit an employee okay so now to edit an employee you can see that we've got two combo boxes there that we need to full we need to full choose employee and we need to full employee job so that will be a list of all the employees and this will be the list of all the jobs so now again if we go to source I want to have a few declarations there at the top so I'm gonna have all those import statements the same as I had in my add employee I'm gonna have them at the top and I'm also gonna have those three declarations why do I have to have both jobs and employees because in the design I want to place the employees there and all the jobs they again okay so that is then let's just have this one also we can also have populated ray list because we're going to use that exact same methods I'm gonna copy all of those go to design and then inside of inert components I will paste it there and that will create new ArrayList and the new ArrayList now populate ArrayList there's copy that method also we're gonna use it again so I'm gonna copy that whole method there we go and then after edit employee we can call that method okay so what do we have in the Edit employee if you save now you should have not have any errors so right at the start we've got our two array lists with the decimal format so obviously we initialize the ArrayList then we call populate ArrayList populate ArrayList we'll go to the jobs data file and add all the jobs to the jobs ArrayList also it will go to the employees data file and add all the employees to the ArrayList so now in this application or when this file starts this frame starts we've got all the employees and we've got all the jobs we just need to set them now to the two combo boxes okay so now what I want to do then again like we did here let's go to the top like we did in this part we're gonna copy that whole part again and then after populating the ArrayList we can do the same thing the jobs array will be going to all the jobs placing all the jobs in the job array and going to jcombobox but now it's not jcombobox one this one is jcombobox two so this part will then be just become a 2d okay so now i'm going to do the exact same thing to set all the employees so I'm gonna copy that whole thing day and let's call this one EMP array and that one should be employees dot size it's gonna be employees there as well and the same day in fact let's let's redo this on go to that one now and it's gonna be jcombobox one so what do we want to have not in the jobs right now but in the employee array so in the employees array what we want to show thee is maybe just if you go to design what do we want to choose therefore that maybe just showing the guys username or not username his name and his surname so what we'll have there is to call gate gate I first gets the employee object and then on the object we can call gate name so let's say name and then a comma and then maybe his surname or maybe he's surname first and then his name it's up to you what you want to do this so I'm gonna say employees then that gate that gets the object go to I and I'm gonna say dot get surname that gets me the surname so that's what I want to sit there and I'm sitting jcombobox one to all the employees so let's run this quickly and see if it works so we're going to go to edit edit employee there we go I've sit said the same thing at the top and the bottom so let me just see they Jimbo okay so there is it should be EMP array employees array and let's run it again it's going to edit employee and there you can see there's all the employees John Rambo there's all the jobs ok so now let's just gonna add a few more employees so let's say Chuck Norris Chuck Norris should be a programmer and his staff number is four five six seven save successfully saved go to edit and you can see John Rambo and Chuck Norris okay so now the next part is if the user chooses let's say Chuck Norris V then I want to update all four fields at the bottom I want to set the employees name the surname whether it's a teen or Oprah and the stove number I want to set all the details to these fields so how do we do that so obviously it's a click event that's part of this combo box so in order to get to the click event for the combo box go to design double click on your combo box and where your cursor is at that will be what happens when you make a selection inside of your combo box so if you make a selection in the combo box what do we want to do now so the first thing that we want to do is to go and check which index is selected in the combo box so that I know which employee is selected so I'm gonna say selected selected index equals and now if you go to design again this is jcombobox 1 or you can also see it in the coding jcombobox someone actually performed so I'm gonna go to J combo combo box 1 dot get selected index and that gets me the index that was selected and now I know which employee I'm actually talking about because if I go to my employees ArrayList now at that exact same index I will get that employee that I'm talking about ok so now what we want to do in the design now if the user then takes a specific employee I want to set his name to jtextfield 1 so I'm gonna go to J text field 1 dot say text and I'm gonna set the text to employee or employees which is the ArrayList dot get the object for me at that selected index that gives me the the object for the one that's selected and then what do I want to do with that object I want to go and get the name ok so that will be get the name and then on the next jtextfield let me just go to design again which is the surname which is jtextfield - so - J text field - I want to set the text as employees dot get again selected index but now I want to call get surname and that gives me the same time for that person ok now go back to design we're gonna do this one now let's do the staff number first which is jtextfield 3 so J text field 3 dot set the text and the text should be well exactly the same thing until they but then we want to call gate staff number then just make sure about that method dot gate staff number and that gets the staff number but we because the staff number is returning an integer and you want to set some text you can just add a double quotation mark li and then it will sit it so if you run it now we're gonna get to that combo box now if you run this now and you go to edit employee and you make a selection you can see it it enters Chuck Norris's data and clicking on john rambo will enter John Rambo's values except for his job at this stage so you can see also that when you start off with this application if you start right at the beginning you can see that this one actually doesn't show the data for John Rambo so maybe what we want to do right at the start is after inert components they we can go and say or maybe even after sitting all of these let's go right at the bottom we will say jcombobox 1 dot set selected index and we're gonna set the selected index as 0 so let's just try that one out so basically what we're saying is we're gonna select that one at position 0 and you can see by selecting at at position 0 it's as if I clicked on it myself so you can see now running it going into it if it's on General John Rambo going to show the info for John Rambo right at the start okay so now if I click on Chuck Norris it shows Chuck Norris's data and now the only one that we need to go and have a look at is this employee job they okay so let's go back to design again let's make sure that we go back to this one to double click on that once he's back you back to the code so we've said what should happen with the text fields now remember that in this object now we can have a job I'm just gonna deteriorate job object very quickly again so a job job equals and then I'm gonna go to this employee that was selected so I'm gonna call that get selected index now that gets me the employee object and then on that object I'm gonna call gate job and that gets me the job that was saved for that employee and now what I want to do is to quickly run through a for loop integer I equals 0 I less then and I'm gonna run through the jobs ArrayList that's all the jobs and then I'm gonna say I plus plus they to run through all of the jobs and what I want to do there is to check what is the index in my jobs ArrayList for this specific job of the employee so I'm gonna say if jobs dot get I dot equals this new job of mine right so if the two job objects are the same then I know it's at that specific in index so let me just have an index value there int index and then we're gonna set the index they index equals and it's gonna be I just to save the index and if I got the index I can just call break which will break me out of the for loop and then I can carry on so now I've got the index of the job so first I go to the employees ArrayList the selected one on this first combo box that selected value go to that specific employee and get his job then run through the jobs ArrayList because in the jobs arraylist from the jobs arraylist we've set this one okay so in the jobs ArrayList we get the job at a specific position and taste it for this job they if it's found we know where it's found and that's the exact same index that we placed on the second combo box right so now after we've got that one we can basically just can say jcombobox and that one is the second jcombobox two dots it selected index and it will be set to index let's make that one zero right at the start just to make sure but in any case we will find it because it's one of the of the jobs so it won't be a problem but just initialize it to zero they okay then if you run it now let's just see if it works now right so let's go to edit and it employee you can see John Rambo Chuck Norris is also a programmer so it seems as if both are saved as a programmer let's just save one as cleaner pizza Pollock he's a cleaner and east of number six eight nine six eight seven eight nine zero save successfully saved so if we go to edit employee let's go to pizza Pollock you can see he's a cleaner and Chuck Norris as well as John Rambo is both programmers they just add another cleaner very quickly it's Sam Smith whatever cleaner staff number say successfully saved so if we go to it employee now let's go to Sam Smith you can see it's also cleaner same with Peter Pollock right so I think this system is now working 100% and now we've got two buttons at the bottom so you can see even if I click on John Rambo and I want to change him now to cleaner I'm able to do it because there's no click event on this on this specific one so if I go to Sam Smith it will change that one again but I'm able to change all of these values and click on save and that you change that specific object so if I click on save they what should happen and if I click on delete what should happen right so if I click on save I want to I want to basically go to this object and make the changes if I click on delete I want to go to this object remove it from the ArrayList and then save back the data ok so let's just quickly have a look at that one if you go to design let's go to the Save button first so where's the Save button there it is now before we do anything else you'll remember in my add employee we had save employees to file so I'm gonna copy that one and go to edit employee and add that method as well so maybe just there is some way and this save employees to file you can see it goes to the employees data file using the output stream going to the employees ArrayList and saving everything in the ArrayList ok so we're gonna use that same one let's go back to design double click on your Save button it will take you back to what we need to do here so if we click Save now we want to basically make sure that we get all the data again from the user so it's exactly the same as saving the data in the add employees so if you click on double click that Save button it's basically this exact same thing so I'm gonna copy this quickly and go to edit employee double click on the Save button and I'm gonna paste the code in yet it's just gonna be a bit different now because if we go to design now we've got a text field jtextfield one takes field two and text field 3 so let me just check the a text field 1 2 & 3 we want to check if they're not empty we're gonna say please enter all fields then what are we gonna do then actually for the spot I I think I should do something totally different here so let's remove all of this now let's just go back to design now so if the user clicks on the Save button we first check that these fields are not empty so now the first thing we need to do is to make that we working with this object that was selected thee so I think let's just get that selected object again so basically gonna do the same as we did they by saying the selected index should be jcombobox 1 dot get selected index and then I can start working with a specific employee now so on that employee employee or employees we're gonna say dot get eye or not I selected index that gives me that specific employee and then you can remember that we've got some setter methods day so then I'm gonna set the name to whatever is typed in jtextfield 1 so I'm gonna say J text field 1 dot K text and then I'm gonna pass in or just gonna call the true method a ok so that is setting going to the the object that was selected and setting the name to whatever is typed in jtextfield 1 then I'm gonna also say just copy this line selected index dot set name so we're gonna set the surname as well to be dot KITT you see which one is the surname it is jtextfield too so we're gonna go to J text field to dot K take stotram and then we're gonna also set let's say set the staff number which is the easier one to do so we're gonna say set staff number let me just make sure about that method dot set staff number there we go and the staff number is at this one jtextfield 3 so we're gonna say jtextfield 3.8 Eckstrom but again this is an integer so reason we need to convert this to an integer so we're gonna say integer dot parse int and we convert that one to an integer okay so now we've got we've changed the employees name his surname his staff number and now we want to change his job to change the job I'm just going to create a job object quickly so we're gonna say job job equals and then I'm gonna go to the jobs ArrayList and I'm gonna get a job at a specific position and the specific position that I'm talking about will be the position that we are in this specific combo box also so that will be the job at combo box too so I'm gonna go to jcombobox to start get selected index because that gets that selected index just say the kid that gets me the specific index for the the jcombobox way that one was selected and it should be the same in my jobs array let's just see what's going on there now sorry two brackets the attained okay so creating a new job object go to the jobs ArrayList but it should be at the position that the combo box two is selected is it the same as the position inside of my ArrayList okay so now I've got the job that the new employee should have or the existing employee should have so I'm gonna say employees dot get again just paste that line again dot get selected index dot said that now in this case said the job and the job should be set to this job that I just created day okay and then we have saved everything on that new employee object or that existing employee object so we go to the object that was selected and we used the setters to change it and then after setting it or off the changing and we can then call this method now where is that method now save employees to file to save the new data to the file okay so we're gonna go save employees to file and that will save all my employees to the file again right let's taste this one quickly out and we run it go to edit employee so there's John Rambo so John Rambo is currently a programmer so let's change John Rambo to a cleaner and we say save successfully saved okay go back let's see if John Rambo is now a cleaner yes he is okay so let's change Sam Smith to be Sam Peterson click on save so if successfully go back there Sam Peterson okay so I think the save or the change function is working 100% let's quickly have a look at the delete function D so if I want to delete something now from that ArrayList so we're gonna go to delete double click on delete again the first thing we want to do is to get which index was selected so I'm gonna use that little single line again and we just make sure them in the correct method now delete double click delete yes right so the first thing we do is to get the selected index from jcombobox 1 so this which what is the employee that we want to delete so it's at index number one and then I want I know that in my employees ArrayList I can get that specific one or I can remove it by just calling remove and at which index do I want to remove it and I want to remove it at the selected index the same index that we chose day so that removes my employee then from that ArrayList and then I just can just call that method again save employees to file right and that saves my employee to the fall again or mine please the fall after that one has been removed let's run it quickly and see how it works go to edit employee let's say we want to delete Chuck Norris now I'm gonna say delete we just we should just change that method baby may be successfully saved okay if you go back to edit employee Chuck Norris is not there anymore okay so maybe for this method we are we now save to file save him please to file let's have a second one day second method save employee to fall and I'm gonna add a delete day so we will say successfully deleted okay so it's saving please to file delete oh is that one now look at the delete button again right there it is instead of save in please to file I'm gonna say delete until the day right so if you run it again now it will show us the message it's been deleted all right so let's delete John remember they delete successfully deleted go back John Rambo's not there anymore and that's the simple application on how you can work with employees saving data deleting data displaying them on different combo boxes so I hope you have learned something from this video
Info
Channel: Johan Jurrius
Views: 116,031
Rating: 4.9506173 out of 5
Keywords: Java, GUI
Id: GZ9MT2myBf8
Channel Id: undefined
Length: 97min 5sec (5825 seconds)
Published: Mon Sep 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.