Saturday, April 11, 2009

Monday April 13th: Improving our "browser"

Gentlemen,

If you will recall, last week, before Evan made some noise with you and I showed you a little bit about JToolbars, we wer working on a kind of "browser": not exactly Firefox, but it was a start. I had told you that at some point, I would show you how to modify this program so that Favorites you added would STAY in your favorites list, by using some basic File reading and writing capabilities.

Before I go any further, I want to point out from the beginning that there are some obvious "bugs" in the way this program is setup. My purpose in doing this is to get you to think about how YOU would fix the errors, as well as being able to look for them in the first place. If you don't know there's a problem, it's kind of like that giant squid in the Family Guy episode: it's there, whether you acknowledge it or not.



Also, there's a file that I used in this version of the program which you can download here:

http://www.box.net/shared/n32n5qk2qs

It's called "addys.pml". Later on, you can modify this file, but for now, you can just use it as we progress through this lesson.

so OK, here we go:

The improvement to be made today involves being able to REALLY save favorites to a list. In the most recent incarnation of this program, you were able to use an "Add to Faves" button to add a particular website address to your JComboBox; but when the program closed, all those newly added Favorites were lost.

The solution is to use some elemental File writing and reading capabilities to your program. If your favorites list is built by reading from a separate text file, then it follows that anything you WRITE to that file during the course of running the program should be available the next time you run it. Here's what I did:

In your constructor, you should have a code block that looks like this:
webURL = new Vector();
webURL.addElement("
http://www.google.com");
webURL.addElement("
http://www.thinkgeek.com");
webURL.addElement("
http://www.yahoo.com");
webURL.addElement("
http://www.foxnews.com");
You need to comment out that entire block!
Well now, if you do that, your Favorites list will be empty. So, we've gotta find some other way of populating it. Here's what I did:

Just below the stuff I commented, I made a method call:
readFilesMakeFaves(); //call this method

This means I have to create the method that will be called; just make sure you don't embed this thing inside another method. That would be bad. Here is what my method looks like:

private void readFilesMakeFaves( )
{
webURL = new Vector();//create vector
//add to that vector using file read
try {
BufferedReader in = new BufferedReader(new FileReader("addys.pml"));
String str;
while ((str = in.readLine()) != null)
{
System.out.println(str); //show whats going into file in system console
webURL.addElement(str); //add to the vector the line just read
}
in.close( ); //stop reading
}
catch (IOException e)
{System.out.println("file not found or is null"); }
}//end readfilemakefaves method

So what this filereader does is to open up the file called "addys.pml", and read it one line at a time. As it does so, it adds each line to the JComboBox. It also prints out each line to the System console. So far, so good!

The next step is to modify the "Add Faves" button so that it can add a favorite as a line into the "addys.pml" file. This will mean that even if you close the program and then re-start it, the new favorites places that has been added will STILL be in the list, which is an improvement over the previous system. Here is how I did it:

private void addFaves()
{
//read into the addy.pml file the address in JTextField
try {
//open a writer to append to end of addys.pml file which stores faves
BufferedWriter out = new BufferedWriter(new FileWriter("addys.pml", true));
out.newLine(); //drop to next line
out.write(WhereTo.getText()); //write in contents of JTextField
out.close(); //stop writing fer cryin' out loud
}
catch (IOException e) { }


OK, here's what I want you to do after you've saved, compiled and run this thing:

A) At some point, I will want you to add a JToolbar to this program which will have graphical buttons that provide another way of doing the stuff we've already been doing with this programB) There's at least one glaring omission with this program, something it should do but doesn't. Figure out what it is, then figure out YOUR way to fix it.

C) After you've done both A and B above, please make a discussion post describing the problem you found and your solution.

D) Please email your completed program before the end of the class.

Cheers,
Uncle Paulie

No comments:

Post a Comment