Viewing Html file..

hello, again.. i have this program named WebStrider.java

that works as a simple browser.. now i have a file named

simple.html found on my pc at d:\strider\. my question is

how can i make my program able to view the said .html file?

what code should i add to make this happen? thanks,..

here is my code..

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

publicclass Papyrusextends JFrame{

Viewer viewer;

JTextField txtfld;

JButton button, config;

Icon icon;

JPanel companel;

public Papyrus(){

super("Let's write on Papyrus!");

Container c = getContentPane();

companel =new JPanel();

companel.setLayout(new GridLayout(4,1));

viewer =new Viewer();

txtfld =new JTextField();

button =new JButton("SEND");

//button.nNemonic(S);

//on = new ImageIcon(guestbook_logo.png);

config =new JButton("Configuration");

companel.add(txtfld, BorderLayout.SOUTH);

companel.add(button, BorderLayout.SOUTH);

companel.add(config, BorderLayout.SOUTH);

c.add(viewer, BorderLayout.CENTER);

c.add(companel, BorderLayout.SOUTH);

setSize(500, 400);

show();

}

publicstaticvoid main(String[] args){

Papyrus app =new Papyrus();

app.addWindowListener(

new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

System.exit(0);

}

}

);

}

class Viewerextends JPanelimplements ActionListener{

JEditorPane content;

JTextField addressbar;

JPanel panel;

public Viewer(){

panel =new JPanel();

panel.setLayout(new GridLayout(2,1));

addressbar =new JTextField(20);

addressbar.addActionListener(this);

content =new JEditorPane();

content.addHyperlinkListener(

new HyperlinkListener(){

publicvoid hyperlinkUpdate(HyperlinkEvent e){

if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)

getThePage(e.getURL().toString());

}

}

);

panel.add(addressbar, BorderLayout.NORTH);

panel.add(new JScrollPane(content), BorderLayout.CENTER);

add(panel, BorderLayout.CENTER);

}

publicvoid actionPerformed(ActionEvent e){

getThePage(e.getActionCommand());

}

publicvoid getThePage(String urladdress){

//setCursor.getPredefinedCursor();

try{

content.setPage(urladdress);

}

catch(IOException io){

JOptionPane.showMessageDialog(null,"ERROR VIEWING PAGE");

}

}

}

[4852 byte] By [strider_hiryu007a] at [2007-11-27 8:56:04]
# 1
java.io.FileReaderjava.io.FileInputStreamjava.io.BufferedReaderjava.io.InputStreamReaderA combination of any of those will do the trick.
Navy_Codera at 2007-7-12 21:18:19 > top of Java-index,Java Essentials,New To Java...
# 2
so, i have to use any of those packages. but what method should i use?can they please just give examples of codes, in order for me to know what to do..
strider_hiryu007a at 2007-7-12 21:18:19 > top of Java-index,Java Essentials,New To Java...
# 3

Here's an example:

import java.io.*;

class Test {

public static void main(String[] argv) {

File f = new File("test.txt");

try {

BufferedReader br = new BufferedReader(new FileReader(f));

String line;

while( (line = br.readLine()) != null ) {

System.out.println(line);

}

br.close();

} catch (FileNotFoundException fnfe) {

System.out.println("File (" + f.getPath() + ") not found.");

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

}

Navy_Codera at 2007-7-12 21:18:19 > top of Java-index,Java Essentials,New To Java...
# 4

StringBuilder sb = new StringBuilder();

FileReader reader = new FileReader("C:\whatever\yeah.html");

int ch = reader.read();

while (ch >= 0) {

sb.append((char)ch);

ch = reader.read();

}

String content = sb.toString();

Of course, it'd be best to follow Navy_Coder's example in catching the exceptions and closing the stream.

Message was edited by:

myncknm

myncknma at 2007-7-12 21:18:19 > top of Java-index,Java Essentials,New To Java...