hi friends i have 2 do a javabrowser where it has 2 display images and also text(both plain and html) and i have 2 do it in pure
java(ie without using advanced java concepts.
i'm able 2 display html and plain text but images dot seem 2 work,also the back ,forward buttons dont take me 2 the previous and next pages,pliz help,i'm including the import java.io.* ;
import javax.swing.* ;
import java.awt.* ;
import javax.swing.event.* ;
import java.awt.event.* ;
import java.net.*;
import java.awt.*;
import java.applet.*;
import java.util.*;
public class JavaBrowser extends JFrame implements HyperlinkListener, ActionListener {
private JLabel addrLabel = new JLabel ( "Address:" ) ;
private JTextField addrText = new JTextField ( "http://www." ) ;
private JButton goButton = new JButton ( "Go" ) ;
private JEditorPane browser = new JEditorPane ( ) ; // The main HTML pane
JToolBar jtbar =new JToolBar();
JButton back;
JButton forw,exit;
static Image img;
public JavaBrowser ( ) {
// Set the title for the frame
setTitle ( "Java Browser" ) ;
// Set the position and size of frame
setBounds ( 10, 10 , 500, 500 ) ;
/* Make the JEditorPane non-editable so that when user
clicks on a link then another page is loaded. By default,
JEditorPane is editable and works as a HTML editor not as
a browser. Make make it work as a browser we must make it
non-editable */
browser.setEditable ( false ) ;
/* Add a hyperlink listener so that when user clicks on a
link then hyperlinkUpdate ( ) method is called and we
will load another page*/
browser.addHyperlinkListener ( this ) ;
//Put the address text filed and button on the north of frame
JPanel panel = new JPanel ( ) ;
JPanel pane2 = new JPanel ( ) ;
panel.setLayout ( new BoxLayout ( panel, BoxLayout.X_AXIS ) ) ;
pane2.setLayout(new FlowLayout(0));
pane2.add(jtbar);
panel.add ( addrLabel ) ;
panel.add ( addrText ) ;
panel.add ( goButton ) ;
back=new JButton(" BACK ",new ImageIcon("back.gif"));
forw=new JButton(" FORWARD ",new ImageIcon("forw.gif"));
exit=new JButton(" EXIT ",new ImageIcon("exit.gif"));
back.setVerticalTextPosition(AbstractButton.BOTTOM);
back.setHorizontalTextPosition(0);
forw.setVerticalTextPosition(AbstractButton.BOTTOM);
forw.setHorizontalTextPosition(0);
exit.setVerticalTextPosition(AbstractButton.BOTTOM);
exit.setHorizontalTextPosition(0);
jtbar.add(back);
jtbar.add(forw);
jtbar.add(exit);
// Add the action listener to button and address text field so that
// when user hits enter in address text filed or presses the Go button
// then new page is displayed
addrText.addActionListener ( this ) ;
goButton.addActionListener ( this ) ;
back.addActionListener(this);
forw.addActionListener(this);
exit.addActionListener(this);
// Add the panel and editor pane to the frame
Container cp = getContentPane ( ) ;
// Add the panel to the north
cp.add ( panel, "North" ) ;
cp.add ( new JScrollPane ( browser, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS ) ) ;
cp.add(pane2,"South");
}
//4 displaying image
public class SimpleImageLoad extends
Applet {
Image img;
public void init()
{
img=getImage(getDocumentBase(),getParameter("img"));
}
public void paint(Graphics g)
{
g.drawImage(img,0,0,this);
}
}
public static void main (
String[] args ) {
String initPage = "" ;
JavaBrowser jb = new JavaBrowser ( ) ;
jb.show ( ) ;
// Check if user has specified any command line parameter
if ( args.length > 0 ) {
// Read the first argument
initPage = args[0] ;
// Display the page
jb.displayPage ( initPage ) ;
}
}
public void displayPage ( String page ) {
// Check if user has specified any command line parameter
if ( page != null && page.trim().length() > 0 ) {
// Set this address
addrText.setText ( page ) ;
/* User may specify one of the following
1. A relative path for a local file
2. An absolute path for a local file
3. A URL
Check for a valid user input
*/
File localFile = new File ( page ) ;
// Chgeck if the file exists on the dist
if ( localFile.exists ( ) && localFile.isFile () ) {
/* Check if user specified the absolute path
Add the file protocol in front of file name */
page = "file:///" + localFile.getAbsolutePath ( ) ;
try {
browser.setPage ( page ) ;
}
catch ( Exception e1 ) {
// Not a valid URL
browser.setText ( "Could not load page:" + page + "\n" +
"Error:" + e1.getMessage ( ) ) ;
}
}
else {
// Maybe user specified a URL
try {
URL url = new URL ( page ) ;
browser.setPage ( url ) ;
}
catch ( Exception e ) {
// Not a valid URL
browser.setText ( "Could not load page:" + page + "\n" +
"Error:" + e.getMessage ( ) ) ;
}
}
}
else {
browser.setText ( "Could not load page:" + page ) ;
}
}
public void hyperlinkUpdate ( HyperlinkEvent e ) {
/* Get the event type for the link. There could be three types
of event generated by user actions on a link.When user's mouse
enters a link then ENTERED event is triggerd. When user clicks the
link the ACTIVATED is triggered and when user exits the link then
EXITED is triggerd.*/
if ( e.getEventType ( ) == HyperlinkEvent.EventType.ACTIVATED ) {
try {
// Loads the new page represented by link clicked
URL url = e.getURL ( ) ;
browser.setPage ( url ) ;
addrText.setText ( url.toString ( ) ) ;
}
catch ( Exception exc ) {
}
}
}
public void actionPerformed ( ActionEvent e )
{
if (e.getSource()==exit)
System.exit(0);
// Load the new page
String page = "" ;
try {
// Get the new url
page = addrText.getText ( ) ;
// User may eneter a file name or URL. displayPage handles both of them
displayPage ( page ) ;
}
catch ( Exception exc ) {
browser.setText ( "Page could not be loaded:" + page + "\n" +
"Error:" + exc.getMessage ( ) ) ;
}
}
} code
CODE 4 JavaBrowser