Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7985573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:35:43+00:00 2026-06-04T11:35:43+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:35:45+00:00Added an answer on June 4, 2026 at 11:35 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a simple web proxy, for exercise. Here's the code I
I want to write a simple web framework myself using WSGI, Python. I am
I want to write a simple task which will update and commit source code
I want to write a simple web service in groovy/grails which will retun some
I have written a simple web server in C. I want to write a
I'm playing a little with Python 3.2.2 and want to write a simple web
I want to start a simple web server locally, then launch a browser with
I want to write a simple python web application to provide a gui to
I want to write simple script to copy/backup directory then remove on server startup.
I want to write a simple DSL in C#. Nothing too complicated. I'm looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.