import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QBrowser implements ActionListener, Runnable {
private JFrame frame;
private JButton go;
private JEditorPane content;
private JTextField url;
private JLabel statusLine;
// default constructor
public QBrowser () {
buildBrowserInterface();
}
private void buildBrowserInterface() {
frame = new JFrame("Q's Browser");
// on close, exit the application using System.exit(0);
frame.setDefaultCloseOperation (3);
url = new JTextField("", 25);
go = new JButton("Go Get It");
go.addActionListener(this);
JPanel controls = new JPanel(new FlowLayout ());
controls.add(new JLabel("URL:"));
controls.add(url);
controls.add(go);
content = new JEditorPane();
content.setEditable(false);
// HTML text. Use the kit in the class javax.swing.text.html.HTMLEditorKit, which
// provides support for HTML 3.2
content.setContentType("text/html");
content.setText("
Q's BrowserCopyright (c) 2002 Qusay H. Mahmoud
");
statusLine = new JLabel("Initialization Complete");
JPanel panel = new JPanel(new BorderLayout (0, 2));
frame.setContentPane(panel);
panel.add(controls, "North");
panel.add(new JScrollPane (content), "Center");
panel.add(statusLine, "South");
frame.pack();
frame.setVisible(true);
}
/**
* You cannot stop a download with QBrowser
* The thread allows multiple downloads to start
* concurrently in case a download freezes
*/
public void actionPerformed (ActionEvent event) {
Thread thread = new Thread(this);
thread.start();
}
// this is the Thread's run method
public void run () {
try {
String str = url.getText();
URL url = new URL(str);
readURL(url);
} catch (IOException ioe) {
statusLine.setText("Error: "+ioe.getMessage());
showException(ioe);
}
}
private void showException(Exception ex) {
StringWriter trace = new StringWriter ();
ex.printStackTrace (new PrintWriter (trace));
content.setContentType ("text/html");
content.setText ("" + ex + "
" + trace + "
");}
/**
* The URL class is capable of handling http:// and https:// URLs
*/
private void readURL(URL url) throws IOException {
statusLine.setText("Opening " + url.toExternalForm());
URLConnection connection = url.openConnection();
StringBuffer buffer = new StringBuffer();
BufferedReader in=null;
try {
in = new BufferedReader(new InputStreamReader
(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
buffer.append(line).append('\n');
statusLine.setText("Read " + buffer.length () + " bytes...");
}
} finally {
if(in != null) in.close();
}
String type = connection.getContentType();
if(type == null) type = "text/plain";
statusLine.setText("Content type " + type);
content.setContentType(type);
content.setText(buffer.toString());
statusLine.setText("Done");
}
public static void main (String[] args) {
QBrowser browser = new QBrowser();
}
}