Tuesday, 18 October 2011

Java: Completing the Text Editor Functionality I

Well, we have our text editor, which looks like:


Now, we need to add functionality to the buttons, but we want our methods of save/load etc. to be just that, their own methods, which the buttons call, so first, we need to actually make the methods, and have the action listeners come in later!

Let's go in order, starting with Load. This is some new material, so pay attention: JFileChooser is what we're using. This took me a while to figure out, too, so we`ll only be doing Load today, Save will be easier with this knowledge, so I can probably do the remaining 4 tomorrow!

I`ll provide the source code at the end, complete with comments, but let`s go through it line by line here, first. As you can see by the line numbers, I`ve just tacked this onto the end of the GUI code.

Line 175: The JFileChooser is, as you might expect, what we`re using to select files to load!
Lines 176 -177: This checks to see if a file has been selected to be opened, the approve option in this case being "Open".
Line 178: This creates a file object populated with the information of the selected file.
Lines 179-183: Remember these readers from the input/output section? I hope so, 'cause they're basically what's going to load and save our files. The try-catch block is there to catch IOExceptions that the BufferedReader can throw!
Lines 184-188: These next lines are saying: "While there's still lines in the file, add them to our StringBuilder, then get the next line. Set the output to the string representation of the StringBuilder." Note that this is incredibly inefficient, since I misplaced the text setter. While in the loop, it sets text for every new line. If placed after the while-block, it only sets text once, and that's much easier!
Lines 189-190: This is just to have a dialog box pop up and alert the user to the issue, instead of quietly keeping track of it somewhere, leaving users non-the-wiser about what's just happened.
Lines 192-193: This is what will happen if the approve button is not hit. "Cancel", that is. It's just there to let the user know they hit the (presumably) wrong button.

At this point, you just want to go to the Load Button's events and tell it to load();

Thar, simple, given what we've learned so far (and yet it took me like 2 hours to figure it out)!
Here's the source code if you want to look at it in more detail, or check out the program as is so far, for yourselves:

Questions welcome! Comment, follow, subscribe, share etc, and see you tomorrow!

And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 

5 comments:

  1. Like Murdoch said, i love when a plan comes together.

    ReplyDelete
  2. Always wanted to learn that stuff. I'm gonna follow and anticipate more!

    ReplyDelete
  3. This wasn't as difficult as I thought. (:

    ReplyDelete
  4. The next 4 buttons shouldn't take you as long now. :P

    ReplyDelete