Creating a web browser in Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Creating a web browser in Java

  1. #1
    Join Date
    Dec 2005
    Location
    Bangalore
    Posts
    13

    Arrow Creating a web browser in Java

    Hi,
    Anybody know how to create a web browser in java.

    Thank you

  2. #2
    Join Date
    Jun 2004
    Posts
    199
    Do you mean actually write a component that acts like a browser? You will need to implement HTML and JavaScript and ... . You can look at some opensource java browser projects to get started.

    Do you mean use a browser component? Well there is JDIC, Commercial browser components and JRex.

  3. #3
    Join Date
    Dec 2005
    Location
    Bangalore
    Posts
    13
    Hi, Nice to see your Reply. I want to create a Web Browser Project, Not a componenet. Can you tell me some open source browsers developed in java.
    I dont want to embedd other default browsers.

    After that I want to use character encoding in this project.
    Thank You

  4. #4
    Join Date
    May 2005
    Posts
    75
    Here's an extremely poor quality, unfinished web browser I started a long time ago. It's not much but it might be a start for you
    Code:
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    
    public class WebBrowser extends JFrame {
    
    	public JPanel
    		address_panel, window_panel;
    
    	public JLabel
    		address_label;
    
    	public JTextField
    		address_tf;
    
    	public JEditorPane
    		window_pane;
    
    	public JScrollPane
    		window_scroll;
    
    	public JButton
    		address_b;
    
    	private Go go = new Go();
    
    	public WebBrowser() throws IOException {
    
    		// Define address bar
    		address_label = new JLabel(" address: ", SwingConstants.CENTER);
    		address_tf = new JTextField("http://www.yahoo.com");
    		address_tf.addActionListener(go);
    		address_b = new JButton("Go");
    		address_b.addActionListener(go);
    
    		window_pane = new JEditorPane("http://www.yahoo.com");
    		window_pane.setContentType("text/html");
    		window_pane.setEditable(false);
    
    		address_panel = new JPanel(new BorderLayout());
    		window_panel = new JPanel(new BorderLayout());
    
    		address_panel.add(address_label, BorderLayout.WEST);
    		address_panel.add(address_tf, BorderLayout.CENTER);
    		address_panel.add(address_b, BorderLayout.EAST);
    
    		window_scroll = new JScrollPane(window_pane);
    		window_panel.add(window_scroll);
    
    		Container pane = getContentPane();
    		pane.setLayout(new BorderLayout());
    
    		pane.add(address_panel, BorderLayout.NORTH);
    		pane.add(window_panel, BorderLayout.CENTER);
    
    		setTitle("web browser");
    		setSize(800,600);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    	}
    
    	public class Go implements ActionListener{
    
    		public void actionPerformed(ActionEvent ae){
    
    			try {
    
    				window_pane.setPage(address_tf.getText());
    
    			} catch (MalformedURLException e) {     // new URL() failed
    				window_pane.setText("MalformedURLException: " + e);
    			} catch (IOException e) {               // openConnection() failed
    				window_pane.setText("IOException: " + e);
    			}
    
    		}
    
    	}
    
    	public static void main(String args[]) throws IOException {
    		WebBrowser wb = new WebBrowser();
    	}
    
    }

  5. #5
    Join Date
    Dec 2005
    Location
    Bangalore
    Posts
    13
    Hi,
    thanx for the reply
    But I done this one already. Using JEditorPane.setPage() method we can develop a web browser, but the problem is that it cannot handle javascript and other scripts. It will not detect textfields in http://www.google.com , That is Google serach. But same thing wors fine with http://www.yahoo.com why?? How can I solve the problem??

    Please look at this code.


    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.File;
    import java.io.*;
    import java.util.*;
    import javax.swing.event.*;
    import javax.swing.text.html.*;
    import javax.swing.event.HyperlinkListener;
    import javax.swing.event.HyperlinkEvent;
    import java.lang.*;
    import java.net.*;
    import java.net.HttpURLConnection;
    public class WebBrowser extends JFrame implements ActionListener,HyperlinkListener
    {
    private JEditorPane jep;
    JTextField urlField;
    JButton back = new JButton("",new ImageIcon("undo.gif"));
    final JLabel statusBar = new JLabel(" ");
    JButton Go=new JButton("GO");
    String url = "";

    public WebBrowser(String startingUrl)
    {
    super("Simple Web Browser : Developed in Java ");
    setSize(1035,740);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Container con=getContentPane();
    JPanel urlPanel = new JPanel();
    urlPanel.setLayout(new BorderLayout());
    urlField = new JTextField(startingUrl);
    urlPanel.add(new JLabel("Site: "), BorderLayout.WEST);
    urlPanel.add(urlField);
    urlPanel.add(Go,BorderLayout.EAST);
    Go.addActionListener(this);
    urlField.addActionListener(this);
    jep = new JEditorPane();
    jep.setEditable(false);
    jep.setEditorKit(new HTMLEditorKit());
    jep.setContentType("text/html");
    JScrollPane jsp = new JScrollPane(jep);

    getContentPane().add(jsp, BorderLayout.CENTER);
    getContentPane().add(urlPanel, BorderLayout.NORTH);
    getContentPane().add(statusBar, BorderLayout.SOUTH);
    jep.addHyperlinkListener(new SimpleLinkListener(jep, urlField,statusBar));
    }
    public void actionPerformed(ActionEvent ae)
    {
    Object obj=ae.getSource();
    String url1="http://www.rediff.com";
    if(obj==urlField)
    {
    try
    {
    url=ae.getActionCommand();
    url1=URLDecoder.decode(url);
    jep.setPage(url);
    }catch(Exception e){statusBar.setText("Error: " + e.getMessage());}
    }
    if(obj==Go)
    {
    try
    {
    jep.setPage(urlField.getText());
    }catch(Exception e){statusBar.setText("Error: " + e.getMessage());}
    }
    }
    public void hyperlinkUpdate(HyperlinkEvent e)
    {
    if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
    {
    jep = (JEditorPane) e.getSource();
    try
    {
    jep.setPage(e.getURL());
    }catch (Throwable t){t.printStackTrace();}
    }
    }
    public static void main(String args[])
    {
    //Setting the proxy if needed.
    /*System.getProperties().put("proxySet","true");
    System.getProperties().put("proxyPort","8080");
    System.getProperties().put("proxyHost","192.168.2.1");*/

    String url = "";
    url = "about:blank";
    new WebBrowser(url).setVisible(true);
    }
    public class SimpleLinkListener implements HyperlinkListener
    {
    private JEditorPane pane;
    private JTextField urlField;
    private JLabel statusBar;
    public SimpleLinkListener(JEditorPane jep, JTextField jtf, JLabel jl)
    {
    pane = jep;
    urlField = jtf;
    statusBar = jl;
    }
    public SimpleLinkListener(JEditorPane jep)
    {
    this(jep, null, null);
    }
    public void hyperlinkUpdate(HyperlinkEvent he)
    {
    HyperlinkEvent.EventType type = he.getEventType();
    if (type == HyperlinkEvent.EventType.ENTERED)
    {
    if (statusBar != null)
    {
    statusBar.setText(he.getURL().toString());
    }
    }
    else if(type == HyperlinkEvent.EventType.EXITED)
    {
    if(statusBar != null)
    {
    statusBar.setText(" ");
    }
    }
    else if (type == HyperlinkEvent.EventType.ACTIVATED)
    {
    try
    {
    pane.setPage(he.getURL());
    if (urlField != null)
    {
    jep.setPage(he.getURL());
    urlField.setText(he.getURL().toString());
    }
    }
    catch (FileNotFoundException fnfe){pane.setText("Could not open file: <tt>" + he.getURL() + "</tt>.<hr>");}
    catch (Exception e){e.printStackTrace();}
    }
    }
    }

    }


    Please tell me how can I fix the problems??

    Thank You
    Waiting for your reply

    Sandeep Kumar K
    Email : sandeepkk2005@rediffmail.com

  6. #6
    Join Date
    Jun 2004
    Posts
    199
    It is not a simple thing to do. Try digging on sourceforge for a javabrowser project and look at there code.

    You will have to handle JavaScript and other Scripting. And CSS. And Embedded objects.

  7. #7
    Join Date
    Jan 2006
    Posts
    5
    HI MAN I CAN ALSO HELP AND HAVE GOT A SOLUTION FOR U TRY IT AND TELL ME MAN
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.html.*;

    // The Mini Web Browser.
    public class MiniBrowser extends JFrame
    implements HyperlinkListener
    {
    // These are the buttons for iterating through the page list.
    private JButton backButton, forwardButton;

    // Page location text field.
    private JTextField locationTextField;

    // Editor pane for displaying pages.
    private JEditorPane displayEditorPane;

    // Browser's list of pages that have been visited.
    private ArrayList pageList = new ArrayList();

    // Constructor for Mini Web Browser.
    public MiniBrowser()
    {
    // Set application title.
    super("Mini Browser");

    // Set window size.
    setSize(640, 480);

    // Handle closing events.
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    actionExit();
    }
    });

    // Set up file menu.
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    JMenuItem fileExitMenuItem = new JMenuItem("Exit",
    KeyEvent.VK_X);
    fileExitMenuItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionExit();
    }
    });
    fileMenu.add(fileExitMenuItem);
    menuBar.add(fileMenu);
    setJMenuBar(menuBar);

    // Set up button panel.
    JPanel buttonPanel = new JPanel();
    backButton = new JButton("< Back");
    backButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionBack();
    }
    });
    backButton.setEnabled(false);
    buttonPanel.add(backButton);
    forwardButton = new JButton("Forward >");
    forwardButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionForward();
    }
    });
    forwardButton.setEnabled(false);
    buttonPanel.add(forwardButton);
    locationTextField = new JTextField(35);
    locationTextField.addKeyListener(new KeyAdapter() {
    public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
    actionGo();
    }
    }
    });
    buttonPanel.add(locationTextField);
    JButton goButton = new JButton("GO");
    goButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    actionGo();
    }
    });
    buttonPanel.add(goButton);

    // Set up page display.
    displayEditorPane = new JEditorPane();
    displayEditorPane.setContentType("text/html");
    displayEditorPane.setEditable(false);
    displayEditorPane.addHyperlinkListener(this);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(buttonPanel, BorderLayout.NORTH);
    getContentPane().add(new JScrollPane(displayEditorPane),
    BorderLayout.CENTER);
    }

    // Exit this program.
    private void actionExit() {
    System.exit(0);
    }

    // Go back to the page viewed before the current page.
    private void actionBack() {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    try {
    showPage(
    new URL((String) pageList.get(pageIndex - 1)), false);
    }
    catch (Exception e) {}
    }

    // Go forward to the page viewed after the current page.
    private void actionForward() {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    try {
    showPage(
    new URL((String) pageList.get(pageIndex + 1)), false);
    }
    catch (Exception e) {}
    }

    // Load and show the page specified in the location text field.
    private void actionGo() {
    URL verifiedUrl = verifyUrl(locationTextField.getText());
    if (verifiedUrl != null) {
    showPage(verifiedUrl, true);
    } else {
    showError("Invalid URL");
    }
    }

    // Show dialog box with error message.
    private void showError(String errorMessage) {
    JOptionPane.showMessageDialog(this, errorMessage,
    "Error", JOptionPane.ERROR_MESSAGE);
    }

    // Verify URL format.
    private URL verifyUrl(String url) {
    // Only allow HTTP URLs.
    if (!url.toLowerCase().startsWith("http://"))
    return null;

    // Verify format of URL.
    URL verifiedUrl = null;
    try {
    verifiedUrl = new URL(url);
    } catch (Exception e) {
    return null;
    }

    return verifiedUrl;
    }

    /* Show the specified page and add it to
    the page list if specified. */
    private void showPage(URL pageUrl, boolean addToList)
    {
    // Show hour glass cursor while crawling is under way.
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

    try {
    // Get URL of page currently being displayed.
    URL currentUrl = displayEditorPane.getPage();

    // Load and display specified page.
    displayEditorPane.setPage(pageUrl);

    // Get URL of new page being displayed.
    URL newUrl = displayEditorPane.getPage();

    // Add page to list if specified.
    if (addToList) {
    int listSize = pageList.size();
    if (listSize > 0) {
    int pageIndex =
    pageList.indexOf(currentUrl.toString());
    if (pageIndex < listSize - 1) {
    for (int i = listSize - 1; i > pageIndex; i--) {
    pageList.remove(i);
    }
    }
    }
    pageList.add(newUrl.toString());
    }

    // Update location text field with URL of current page.
    locationTextField.setText(newUrl.toString());

    // Update buttons based on the page being displayed.
    updateButtons();
    }
    catch (Exception e)
    {
    // Show error messsage.
    showError("Unable to load page");
    }
    finally
    {
    // Return to default cursor.
    setCursor(Cursor.getDefaultCursor());
    }
    }

    /* Update back and forward buttons based on
    the page being displayed. */
    private void updateButtons() {
    if (pageList.size() < 2) {
    backButton.setEnabled(false);
    forwardButton.setEnabled(false);
    } else {
    URL currentUrl = displayEditorPane.getPage();
    int pageIndex = pageList.indexOf(currentUrl.toString());
    backButton.setEnabled(pageIndex > 0);
    forwardButton.setEnabled(
    pageIndex < (pageList.size() - 1));
    }
    }

    // Handle hyperlink's being clicked.
    public void hyperlinkUpdate(HyperlinkEvent event) {
    HyperlinkEvent.EventType eventType = event.getEventType();
    if (eventType == HyperlinkEvent.EventType.ACTIVATED) {
    if (event instanceof HTMLFrameHyperlinkEvent) {
    HTMLFrameHyperlinkEvent linkEvent =
    (HTMLFrameHyperlinkEvent) event;
    HTMLDocument document =
    (HTMLDocument) displayEditorPane.getDocument();
    document.processHTMLFrameHyperlinkEvent(linkEvent);
    } else {
    showPage(event.getURL(), true);
    }
    }
    }

    // Run the Mini Browser.
    public static void main(String[] args) {
    MiniBrowser browser = new MiniBrowser();
    browser.show();
    }
    }





    shamshami@yahoo.com

  8. #8
    Join Date
    Nov 2006
    Posts
    1
    thk u -> your code is working -> did something like yours earlier,
    but didn't find my code at the moment -> problem with javascript
    and stuff like css(and other formatting) stays ...

    wkr
    monked

  9. #9
    Join Date
    Nov 2009
    Posts
    2

    setPage replaced by setText......what to do?

    Hi,

    When the setPage() is replaced by setText() method in Mini Browser code,

    the actual url is not being passed to newUrl. Instead it stores null in it.
    How can I rectify this problem?

  10. #10
    Join Date
    Nov 2009
    Posts
    2

    setPage replaced by setText......what to do?

    Hi,

    When the setPage() is replaced by setText() method in Mini Browser code,

    the actual url is not being passed to newUrl. Instead it stores null in it.
    How can I rectify this problem?

  11. #11
    Join Date
    Mar 2012
    Posts
    1

    Project

    hey hi... i want the java code for controlling web browser functions..
    like i need to change the url of already opened browser page using a java code.. and i need to perform next page and previous page functions..
    i want this to be done directly in firefox browser.. not through applets...

Similar Threads

  1. Replies: 2
    Last Post: 10-26-2005, 07:58 AM
  2. Re: Why REST is a better way to do Web Services
    By Michael D. Kersey in forum .NET
    Replies: 2
    Last Post: 08-30-2002, 12:05 AM
  3. Why Web Services are Important
    By Constance J. Petersen in forum .NET
    Replies: 13
    Last Post: 08-28-2002, 10:06 PM
  4. embed a web browser in Java?
    By ilogix in forum Java
    Replies: 0
    Last Post: 03-27-2001, 10:04 AM
  5. SQL in CT
    By Purple girl in forum Database
    Replies: 0
    Last Post: 07-05-2000, 12:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center