Search code examples
javajavascriptswingbrowserjeditorpane

browsing javascript


I want to write a simple web browser in java and here’s my code!

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

 public class WebBrowser extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;

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();
}

}

It works fine for simple html pages but it cannot load JavaScript part of the code! My problem is what should I add to the code to load the javascripts? Thank you!


Solution

  • Swing's default widgets only have very basic support for HTML4 and CSS, with absolutely no support for JavaScript at all (by default). You could potentially use the built-in Rhino JavaScript engine to execute the code, but that would have to be done manually and would be pretty difficult. HtmlUnit uses this tactic to parse HTML pages and execute the JavaScript, but it has generally poor compatibility and completely lacks a renderer, so you'd have to write that yourself (i.e. no display, you only get access to the page content from code).

    There's a few Swing-based browser widgets floating around which embed a Gecko (Firefox) or WebKit (Chrome/Safari) renderer and would thus be able to take advantage of proper JavaScript interpreters, but they're all buggy, expensive, or unmaintained. These would all support JavaScript but they generally use very old versions of the various browser engines and have poor compatibility with modern websites, in addition to lacking cross-platform compatibility.

    Eclipse's SWT project includes a browser widget that appears to be actively maintained, but has a dependence on the SWT libraries and would be somewhat more difficult to use in a Swing application, though it may be possible. SWT is an entirely different UI toolkit from AWT/Swing (which you're currently using) and in order to take advantage of its browser widget, you'd have to find a way to embed it in a Swing app, or use only the SWT toolkit.

    Overall, SWT's browser is probably your best bet for getting a decent browser in Java, but it may still be troublesome to use. Good luck!