|
Lab assignment 6 - NetscrapeIn the last lab assignment of the course you are to write an improved version of Netscape, that not only shows web pages but also makes a table of all links on the page. The result might look like this:
Three advanced swing components should be used: JEditorPane, JScrollPane and JTable. Also, the swing class HTML that parses web pages will play a major role. These are the imports you'll probably use. import java.io.*; import java.awt.*; import java.awt.event.*; import java.net.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.html.*; import javax.swing.text.*; The graphical interface
When writing a graphical program that is not trivially small, it's often a good idea to start with a prototype, i.e. a program that looks like the complete program but lacks most of the functionality. In this case the prototype should show a JScrollPane links=new JScrollPane(new JTable(50,2));It's also best to place the web browser (the center component) in a scroll pane as well, since the entire page will be too large for the window.
Run the program and check that it's possible to write text in all three
components. This is an accidental feature (or bug?) as far as the web
page and link table components are concerned. TextFields are frequently
used for input so they are expected to be editable.
The user enters the web address to go to in the TextField. When the user presses Enter, the The web browser
Your task is now to write a class
When the class is ready, Netscrape can do The surfer - extra assignment
When you try your web browser out you will be annoyed by the fact that the browser does nothing when you click the links. This is not necessary to fix to pass the lab assignment, but counts as an extra assignment. Two things must be done: you have to call
So, write a class The link tableLeave the Netscrape class for a while and write a new program that outputs all links from a web page. It can be called WebCheck and consist of, at least for now, just a main-method. A web page can be printed with the following code String webbsida="http://www.nada.kth.se/~henrik"; InputStream in=new URL(webbsida).openConnection().getInputStream(); InputStreamReader reader= new InputStreamReader(in); while(reader.ready()) System.out.print((char)reader.read());The compiler requires a try-catch since the connection might fail. Instead of reading the input stream, you can create an empty HTMLDocument and let a HTMLEditorKit read the web page into it by
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
new HTMLEditorKit().read(reader,doc,0);
(Without the first statement you'll get a ChangedCharSetException on all swedish sites that explicitly state the character set as iso-8859-1.)
An HTML page consists of different kinds of tags. The tag used for
links is called
When this works, change the program to also output the text between the A-tag and its closing tag by using Instead of printing links and link text, WebCheck should now add them in a JTable, which can be retrieved by Netscrape with the call links=new JScrollPane(WebbCheck.linkTable(addressBar.getText()));Now, your code should be moved from the main method to a method
linkTable() which returns a JTable.
To begin with, put the links and link texts into a
50x2 String matrix and after that create a JTable
from the matrix and a two-element caption vector. Also, give the background
a nice color before returning the JTable.
While you are testing the program, it might be a good idea to have a
The last problem to solve is how to get rid of the old table before
adding a new one when a new URL is requested. One way is to first
call Finally, you can go web smurfing among the links to celebrate that the last prutt assignment is completed. |