Java text editor app 📓

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody it's your bro here hope you're doing well and in this video we're going to be creating a basic text editor in java so sit back relax and enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running let's get started people let's build a text editor so just so you know this isn't going to be anything that great it's going to be of the walmart great value brand of text editors it's not really going to be that sophisticated just something very basic so let's begin by creating a new java project so file new java project i will call this text editor and then click finish and we will not create a module then within the project folder text editor we're going to create two classes file new class i will call my first class just main just because it contains the main method so make sure to check public static void main and click finish then for our second class we're going to create a new class file new class and i will call this text editor and click finish all right so we have two classes one called main and the other called text editor within my main class i'm going to create an instance of my text editor class so new text editor you can give this a name instance like text editor text editor equals new text editor but i don't really think we'll be doing anything with this named instance this should be fine so we have a new text editor then this class is going to extends the jframe class extends jframe and it's also going to implements the action listener interface so that we can handle events we'll need some imports so we'll include these as we go along and since we're implementing an action listener interface we're going to need to add any unimplemented methods so we'll need to declare and define this action performed method but we'll get back to this later we need to create the constructor for this text editor class so let's do that at the top the constructor name is the same as the class name text editor and we're going to create our frame so with frames we usually say this dot set default close jframe dot exit on close so when you click that right x in the top right corner it's going to close out of the program and we should set a title this dot set title and name this whatever you want i'm going to call mine the bro text editor and we can set a size this dot set size and we can pass in a dimension or a width and a height i will set this to 500 by 500 and we can use a flow layout if we want because i like full layouts compared to the standard default border layout manager so this dot set layout or you can set this to null and manually place all the components but i tend to like flow layouts new flow layout and then we'll need an import and we need to this dot set visible to true so here's our window now pay attention to when i run this this will appear in the top left corner if you want this to appear in the middle of your computer screen you can add this line of code this dot set location relative to no so this will appear in the middle of your computer screen or at least close to it much better i sometimes forget to include this line because it's not something i think about all right that is the constructor for now let's create a j text area to add to this frame so let's declare this outside of the constructor but we're going to instantiate it within the constructor so this is a j text area and we will call this text area and within the constructor we'll instantiate this and we'll probably need an import all right so j text area text area equals new j text area now for the time being we're going to add this text area to our frame but eventually we're going to be instead adding this to a scroll pane when we get to that point we just want this text area to be visible while we're working on this and let's set a size for this text area so text area dot set preferred size and we pass in a new dimension new dimension and let's say this will be 450 by 450 something slightly smaller than the size of the frame and let's try this here's our text area the font is kind of small so we'll change that later and if we were to keep on typing it's just going to continue off forever we'll want to set the word wrapping to true so that when the text reaches this bright border it's going to move down to the next line so in order to set that it is text area dot set line wrap and we will set this to true let's also add text area dot set rap style word and that's it oh we set this to true as well okay so then when we run this and we were to type in some text if this touches the right border or gets close to it it's going to continue down to the next line let's also change the default font too so text area dot set font and you can pass in a new font new font pick whatever font you want i'll pick arial i'll make this plain so font dot plain and then a size 20 is a good size and this should be slightly larger this is some text cool for this next section we're going to add a scroll pane that contains a scroll bar and we will be adding this text area to the scroll pin and then adding the scroll pane to the frame so let's declare a scroll pin so outside of the constructor j scroll pane we will call this scroll pin and we'll finish instantiating this within the constructor we'll need an import as well so be sure to include that so after the text area let's create our scroll pane so scroll pane equals new j scroll pane and what do we want to add to the scroll pin we're going to add our text area and then instead of adding the text area to the frame we're going to in place add this uh the scroll pin to the frame instead there's a few things we need to set up with the scroll pin so let's set a preferred size we no longer need to do so with our text area so type in the name of the scroll pane and then we can use the set preferred size on the scroll pin and let's add a vertical scroll bar so in order to do that type in the name of the scroll pin dot set vertical scroll bar policy and there is a constant that we need to type in within the method scroll pane constants dot so let's pick vertical scroll bar maybe always you can also add as needed or never let's set this to always and let's take a look at this and for now i'm just going to change the font to something slightly larger just to test the scroll bar okay let's give it a go so here's our scroll bar it's currently not needed but it's still visible so let's type in some text type in whatever you want all right so as the text is being added to the text area you can see that the size of this text area is expanding and the scroll bar will actually move us up and down this text area so cool we know that this is working then now what we're going to do is allow the user to change the size of the font within the text area so we can best do this with a j spinner so that they can adjust the current size of the font so let's declare a j spinner outside of the constructor so j spinner we will call this spinner font size spinner and we will finish instantiating this within the constructor so font size spinner let's add this here font size spinner equals new j spinner and we can set a preferred size to this so font size spinner dot set preferred size new dimension 50 by 25 should be good and then we can set an initial value so font size spinner since our default font will be 20 we can set this to 20. font size spinner dot set value we will pass in 20. okay so with spinners they use something called a change listener since we're already using an action listener this is going to get a little complex but what we could do is that when we add a change listener to this we can just pass in an anonymous change listener so this part is going to be a little funky but it shouldn't be too bad so we're going to add a new change listener within the constructor or within the method of this add change listener so new change listener and we will need to import this and then add any unimplemented methods so what do we want to do when we actually change the value on our spinner well we're going to change the size of the font within the text area so text area dot set font and we will pass in a new font we're going to keep the font styling the same so we will pass in whatever the text area font is currently so text area dot get font and then follow this with dot get family for our second argument we have to separate this with a comma and then we are going to set this to plane so font dot plane and then the size so we need to get the current size of the spinner so to do that we type in font size spinner dot get value and then we will probably need to cast this as an integer so be sure to add this cast and i need one more parenthesis okay then we need to add this font size spinner to the frame so let's add this before we add the scroll pin so this dot add font size spinner now what happens when we run this we have this spinner it's set to 20 by default let's add some text and then if we were to increase this the size of the text is going to increase as well or decrease now if we set this to something massive like 100 it's going to take out nearly the entire scroll pin and if you were to keep on increasing the size well then we have our scroll bar to actually scroll through this i think i'm going to add a label that just says font next to this spinner here so let's add that real quick j label we'll call this label actually better yet font label font label add this import and let's add this here font label equals new label actually that's a j label and let's set the text right away to just font and that's it and then we need to add this label so this dot add our font label and i think that's a little bit better we just have some clarification as to what this does it just changes the font for this next section let's let the user change the font color of the text area and we're going to need some help with the j color chooser class but first we'll need a button so let's declare a j button that will launch or create this instance of the j color chooser class so let's create a j button and we will call this font color button we'll need an import and we'll need to and finish instantiating this color button so let's add this after the spinner right here is good font color button equals new j button and we can set some text right away by passing in a string to the constructor so let's say color and we will add this button to the frame so this dot add font color button but i think i'm going to add that after the spinner here we will also need to add an action listener to this button so that it does something when we click on it so font color button dot add action listener since we're implementing the action listener interface we can just pass in this now within the action performed method at the bottom we're going to check to see if the event that happens is our font color button so if e dot get source is equal to our font color button then we're going to create a new instance of our j color chooser so j color chooser we will call this color chooser equals new j color chooser and be sure to include these imports now to get the color we're going to need to show a dialog box and we will assign whatever we choose to a color so color color equals color chooser dot show dialogue we can pass in a parent component we don't have one so let's set this to null we can set a title choose a color but it's not necessary and an initial color if you want a default color i'm going to set this to just black color dot black and then we will need to change the current font color so text area dot set foreground we're going to set this to our color and that's it let's try this let's type in some text i'm going to expand this a little bit make it larger and we can select a color let's change this to red and click ok now all of the text within the text area is red this next part is going to blow your mind we're going to let the user pick a font that they want for our text editor so we're going to need the help of a j combo box so let's declare that at the top j combo box let's call this font box and we'll need an import and we'll finish instantiating this after the color button so right here is good font box equals new j combo box with these j combo boxes we can add options to the stray combo box by passing in an array to this constructor but we'll need to create an array of all the available fonts in java so let's do that right before the font box so let's create an array of strings and we'll call this fonts and we need to get all of the current fonts available in java so what we do for that is we can type in graphics environment dot get local graphics environment dot get available font family names and this will take all the fonts available in java and assign them to our array of strings called fonts and within the constructor of the j combo box we're going to pass in this array of fonts then we'll need to add an action listener to this j combo box so font box dot add action listener since we're implementing the action listener interface we can just pass in this with these combo boxes we can set an initially selected value so let's do that so to do that it is fontbox.set selected either index or item so pass in a font name that you want for the default i'm just going to pick arial because that's what i picked originally for this text area for the default text so i'm just going to set this to arial by default then we need to add this font box to the frame and let's add that after the button this dot add font box and then when we run this we have a drop down menu a combo box with all of the fonts available to us however if we were to select one of these we want to actually change the font because right now it doesn't currently do that so within the action performed method we're going to add an if statement to check to see if the event that is passed in is equal to our font box we're going to add another if statement if e dot get source is equal to our font box then what we're going to do is change the current font for the text area so text area dot set font and we will pass in a new font the only thing that we're going to change is the current font family so font box dot get selected item this is going to get the item of whatever we set the font box to but we'll need to cast this as a string so make sure to add this cast right before this for the next argument we can just set this to plane so font dot plane and then we want to keep the font size the same as what it was previously so for the third argument we're going to get the current size of the text area more specifically the font so text area dot get font followed by dot get size this will keep the font size the same and that is it for the font box let's test this this is some text going to increase the font size a little bit and we should be able to change the font now so pick whatever font you like are wingdings available oh yeah but you can't really display them that's a shame alright so that's it for this portion let's add a menu bar for this menu bar i think we'll only have one tab just for file and we'll only be able to open a file save a file or exit so we'll need to create a menu bar a menu and then some menu items so let's add a menu bar first and we need to declare this j menu bar we will call this menu bar and we will need a menu so that's j menu we will call this file menu and we need some items to add to this file menu so these are j menu items or singular just item and we will have an open item item and then a save item and exit item so save item and exit item and be sure to include these imports too now we need to set up this menu bar first so let's add this after the font box i think i'm just going to add a comment just to section off our menu bar area so this isn't necessary but this will help me organize things all right so we'll have a little section for our menu bar so we need to finish instantiating everything so for our menu bar menu bar equals new j menu bar we'll need to create the file menu so that is file menu equals new j menu and we can set the text right away by passing it into the constructor so we will set this to file kind of like the tab on the top toolbar of your ide so it says file i'm just going to mimic that and we'll need to add some items to this file menu so we need to finish instantiating all of these menu items so we have open item equals new j menu item and we will set the text right away and this will be open so we have open save and exit open item save item the text will be save and then we need to exit exit item and exit this next portion has multiple levels to it we need to add all of these menu items to the menu and then add the menu to the menu bar and lastly add the menu bar to the frame so our first step is to add these menu items to the menu so we type in the name of the menu which is file menu and we use the add function and we're going to add all of these individual menu items so we need to begin with adding our open item j menu item followed by save item and exit item so i'm just going to paste this a few times and we have open item save item and then exit item now we need to add this menu to the menu bar so this time we type in the name of the menu bar dot add and we're adding our file menu this time and lastly we need to add this menu bar to the frame but we don't use add we type in this dot set menu bar and we pass in our menu bar and that's it oh it's set j menu bar i constantly make that mistake so this dot set j menu bar and then when we run this we should have a menu bar at the top so we only have one menu one tab and it has three items open save and exit but we want these to do things so we need to add some action listeners to each of these items so let's do that here so we have open item dot add action listener since we're implementing the action listener interface we can just pass in this so this and let's repeat the process for save item and exit item so we're just adding action listeners to all of these items now let's scroll down to the action performed method we're going to create actions for each of the items within our menu so we have if e.getsource is equal to our open item that's our open button basically then we're going to do something and we'll need to do the same process for save item and exit item so we have save item and exit item actually let's fill an exit item because it's only going to take a second to exit out of your program it is just system.exit and we pass in zero and that's it let's try it so i'm just going to type hello this is kind of lame so let's exit let's go to file and exit it does the same thing as that x button in the top right corner of the window uh but you can also do it here too you know file exit so that's an option available to you let's work on save item next now within the if statement of e.get source is equal to save item we're going to need the help of our jfile chooser class and we will select a file location to save our text to so for the first line we're going to create an instance of our j file chooser class so j file chooser let's call this instance file chooser equals new j file chooser and you can also set a default directory of where you want to begin looking for a file location to save so to do that we type in the name of the file chooser file chooser dot set current directory and we pass in a new file with the file path but if you want the default to be your project folder all you have to do is type in a dot and that's it otherwise you can pass in a file path like c colon slash blah blah blah whatever you want but let's just say i want my file path to begin in my project folder so that is just a dot and at this point we need to open a show save dialog and this returns an integer response so we can actually store this within a variable let's call this variable response and it stores an integer value so we need to type file chooser dot show save dialog not open dialog and if you have a parent component you can add that here but we'll just say null for now so we need to check to see what this response is it's going to be either zero or one so we can check that with an if statement so if response is equal to zero but a better way of writing this would be j file chooser dot approve option so that would be better so if they find a suitable file location then we're going to create a new file and a print writer as well so let's just declare a file file and a print writer and we'll call this file out all right so let's finish instantiating this file file equals new file and we're going to get the current path of the file location that we selected for this response for file chooser so we type file chooser dot get selected file dot get absolute path so that's going to be stored within our file and then we are going to print to our file out print writer so we need to finish instantiating this but we're going to probably need to put this within a try and catch block so let's finish instantiating our print writer so file out equals new print writer and we're going to pass in our file and we'll need to surround this with a try and catch block all right now for the second line of the try block file out dot print line and what we're going to print is text area dot get text and it would be good practice if we were to close this print writer so let's add a finally block so at the end of trying this or if we catch an exception we're still going to close our file out print writer so fileout.close and i think we need to initialize this uh so let's change one thing print writer file out equals null and then we're going to instantiate this within the try block so then we can catch any exceptions that occur so now we should be able to save a file someplace within our computer so let's try this let's change the font slightly maybe 30 and a color why not purple and then let's select a font i wonder if there's ink free i think it should be in here uh there it is all right i'll write a poem roses are red violets are blue booty booty booty booty rockin everywhere and we should be able to save this so let's go to file save the default directory is now my project folder but i can save this anywhere let's save this to my desktop and we can pick a file name i will save this as home and be sure to add the file extension so i want to save this as a txt file so that's dot txt you could save this as an html file too if you want to write an html file for a website or something so i'm going to save this as poem.txt and i'm saving this my desktop and this should be saved let's take a look well welcome to my desktop everybody there's a bunch of junk in here but here's that poem that we wrote the font as well as the font color are not going to be saved since this is a plain text file but all of the text is saved from the text editor and here is that poem that we wrote roses are red violets are blue booty booty booty booty rocking everywhere now we can actually edit this save it and then reload this into our text editor program so i actually wrote a haiku for you guys your code is thunder high above and travels far like comment and sub so i'm going to file save and we're just going to keep this as the same name poem.txt now let's go to the open item section of our action performance method and we are going to load this file this time the first two lines of code are the same for the save item portion and we can just copy these just to save some time so it's going to be the same as j file chooser we'll call this file chooser equals new j file chooser and you can set the current directory if you want now an option available to you is that you can apply a filter to only search for certain file extensions so if you want this is how to do that we're going to create a new instance of file name extension filter and we will call this instance just filter equals new file name extension filter but i'm just going to copy this and within the constructor for this file name extension filter we pass in a title as well as a file extension of what we want to look for so if we only want to look for txt files let's say text files this is what's going to appear in the drop down menu and this portion this argument is the file extension so that is txt and in order to apply this filter we need to set the file filter for the file chooser so we type in the name of the file chooser dot set file filter and we pass in our filter which is filter and that's it our next portion is that we're going to show open dialogue and this will return a response so let's store that response within a variable called response so this is normally a one or a zero for the response so into response equals file chooser dot show open dialog be sure you're not clicking show save dialog it's going to be a show open dialog and if there's a parent component you can add that here but we don't have one so let's set this to no then if response is equal to j file chooser dot approve option then we are going to open this file but let's create a new file file file equals new file and within the constructor for this file we're going to pass in file chooser dot get selected file dot get absolute path so let's create a scanner scanner we're going to call this file in kind of like what we did with file out so file in and we'll set this to null for now because we're going to instantiate this within a try and catch block so file in equals new scanner and we are going to pass in our file and then it's going to prompt us for surrounding this with a try and catch block so the second line within the try block we're going to check to see if this file is in fact a file like a legitimate file so if file dot is file then we are going to attempt to read this file so while file in dot has next line then we are going to read this file line by line but we're going to store each line within a variable called line and this is going to be reassigned a value after each iteration of the while loop so string line equals file in dot next line this doesn't add enters to go down the next line when we actually load this so i'm just going to add a new line after each line so this is a new line character and then we are going to append our text area so text area dot append and we are going to append this with our line and then we should close the scanner at the very end so let's add a finally block after the try and catch block finally file in dot close and let's attempt to read that file of the haiku that we wrote currently nothing's within my text editor right well let's open a file let's go to file open and the default directory is set to my project folder but i can look anywhere so that file is on my desktop and since we applied a filter i have either all files that i can search for or just txt files so this will only display folders and txt files so here's my poem poem.txt and i'm going to open this and here's that poem that we wrote your code is thunder high above and travels far like commented sub and then you can change the color and everything too so do whatever you want well everyone that's just how to create a basic text editor there's still way more advanced stuff you can do but since we're just beginners i thought i would hold off on that for now so if you want a copy of all this code i'll post all of this in the comments down below but yeah that's one of a few ways in which you can create a simple text editor in java why are you still watching hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] [Music] you
Info
Channel: Bro Code
Views: 19,390
Rating: undefined out of 5
Keywords: Java text editor, Java editor, Java, text, editor, app, program, ide
Id: NKjqAQAtq-g
Channel Id: undefined
Length: 39min 34sec (2374 seconds)
Published: Thu Sep 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.