This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Enjoy your java HTML browser...
Posted by Kishori Sharan on November 11, 2000 at 2:09 PM
Hi Here is the mini HTML browser in Java. The name of the class is JavaBrowser. Compile the program and then run it. You can also pass command line parameter which is a HTML file to display in the beginning. You can also enter an internet address and it will open the webpage in the frame. I have attached the short description of the program and the code. Thanx Kishori ///////////////////////////////////// Here is the description of program: ////////////////////////////////////// 1. JavaBrowser is the name of the class you will run. It extends Jframe to show the HTML file. The JFrame contains a panel in North and a JEditorPane in center. In panel we have address label, address field and go button. JavaBrowser implements HyperlinkListener so that when user clicks on a link then we can load another page. For HyperlinkListener interface we have implement updateHyperlink ( ) method. It also implements ActionListener so that when user types in a new URL and clicks the GO button or hits enter key then we can load the new page.
2. In constructor of JavaBrowser I have adde3d the panel and JEditorPane described in STEP 1. 3. displayPage ( String page ) method: This method first checks whether the string passed to this is a file name or a URL. If it is a file name then it prefixes "file:///" to the string and then tries to load the page else tries to check if it is a valid URL and tries to load the page from that URL. 4. main ( String[] args ) method: Checks if user has provided any URL at common prompt then passes that URL to displayPage ( String page ) method which in turn tries to load the page. 5. hyperlinkUpdate ( ) method : This has to be implented because JavaBrowser implements HyperlinkListener. This method is invoked when user clicks on a link in HTML page. 6. actionPerformed: ( ) method: This method is invoked when user hits enter in address text field or hits GO button. This method reads the address entered by user and passes it to displayPage method to load the new page. //////////////// JavaBrowser.java ////////// import java.io.* ; import javax.swing.* ; import java.awt.* ; import javax.swing.event.* ; import java.awt.event.* ; import java.net.URL ; 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 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 JEditoPane 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 ( ) ; panel.setLayout ( new BoxLayout ( panel, BoxLayout.X_AXIS ) ) ; panel.add ( addrLabel ) ; panel.add ( addrText ) ; panel.add ( goButton ) ; // 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 ) ; // 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 ) ) ; } 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 ) { // 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 ( ) ) ; } } }
Replies:
|