A simple web browser in Java. It ONLY has support for reading HTML, and as such, anything but a pure HTML page is going to look strange. As I only spent about 10 minutes on this, don't expect hyper links to be navigable.I will probably update the tut to cover that someday. For now, you can type in a url and click 'go' to navigate there.
Here's what I did:
I created a JFrame.
I created a JEditorPane, which displays the HTML.
I created a JScrollPane and put the JEditorPane in it.
I created a JTextField which is used to enter urls.
I created a JButton which is used to call the 'setPage()' method of the JEditorPane.
The code is pretty easy to follow.
Any question, please ask.Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package browser; /** * * @author Tom */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; import java.io.*; public class Browser { private JFrame frame; private JPanel panelTop; private JEditorPane editor; private JScrollPane scroll; private JTextField field; private JButton button; private URL url; public Browser(String title) { initComponents(); //set the title of the frame frame.setTitle(title); //set the default cloe op of the jframe frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set size of frame frame.setSize(800,600); //add jpanel to north of jframe frame.add(BorderLayout.NORTH, panelTop); //add textfield and navigation button to jpanel. panelTop.add(field); panelTop.add(button); //add scroll pane to jframe center frame.add(BorderLayout.CENTER, scroll); //set the frame visible frame.setVisible(true); }//end Browser() constructor private void initComponents() { //create the JFrame frame = new JFrame(); //create the JPanel used to hold the text field and button. panelTop = new JPanel(); //set the url try { url = new URL("http://www.javagaming.org"); } catch(MalformedURLException mue) { JOptionPane.showMessageDialog(null,mue); } //create the JEditorPane try { editor = new JEditorPane(url); //set the editor pane to false. editor.setEditable(false); } catch(IOException ioe) { JOptionPane.showMessageDialog(null,ioe); } //create the scroll pane and add the JEditorPane to it. scroll = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //create the JTextField field = new JTextField(); //set the JTextField text to the url. //we're not doing this on the event dispatch thread, so we need to use //SwingUtilities. SwingUtilities.invokeLater(new Runnable() { public void run() { field.setText(url.toString()); } }); //create the button for chanign pages. button = new JButton("Go"); //add action listener to the button button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { editor.setPage(field.getText()); } catch(IOException ioe) { JOptionPane.showMessageDialog(null, ioe); } } }); }//end initComponents() public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Browser("Simple web browser"); } }); }//end main method. }//end Browser class
Last edited by farrell2k; 06-29-2010 at 05:20 PM. Reason: Spelling correction.
OK, so it navigating links is easier than I thought. To modify the above code to check for hyperlink clicks, we need to add a HyperLinkListener to the JEditorPane. Find the initComponents() method in the code above and add this:
We add a HyperLinkListener to the JeditorPane referenced as editor, and then we override the HyperlinkListener interfaces hyperlinkUpdate() method. In this mehod we use an if statement to call e.getEventType() to see what happened, and if a hyperlink was clicked on, the event type is HyperlinkEvent.EventType.ACTIVATED, so we call the JEditorPane setUrl() method to change the url to the url of the hyperlink that was clicked by passing the argument e.getURL() to the JeditorPane's setPage() method. Easy.Code:editor.addHyperlinkListener(new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) try { editor.setPage(e.getURL()); } catch(IOException ioe) { JOptionPane.showMessageDialog(null,ioe); } }//end hyperlinkUpdate() });//end HyperlinkListener
The HyperlinkEvent.EventType class also has a field named 'ENTERED'. You could create a JLabel and add it to the JFrame and simply include another if statement in hyperLinkUpdate() that sets the text of the JLabel to the URL of the hyperlink your hovering. Many web browsers have this feature.
great dude...
Tested, works. Thank you.
It is nicely explained.
I posted compiled application to
Index of /java/Browser
Thanks for the tutorial. Was pritty cool to try it out and try to modify it))
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks