Siddharth Venu
Siddharth Venu

Reputation: 1388

Java simple web browser weird output

To learn Networking in Java, I followed a tutorial to create a new web browser in NetBeans. Here is the code in ReadFile class:

package WebBrowser;

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

/**
 *
 * @author Siddharth Venu
 * 
 */
public class ReadFile extends JFrame{
    private JTextField addressBar;
    private JEditorPane display;

    //constructor
    public ReadFile(){
        super("Sid Browser");

        addressBar=new JTextField("Enter address");
        //lambda expression instead of anonymous class
        addressBar.addActionListener((ActionEvent event) -> {
            loadData(event.getActionCommand());
        });
        add(addressBar,BorderLayout.NORTH);

        display = new JEditorPane();
        display.setEditable(false);
        display.addHyperlinkListener((HyperlinkEvent event) -> {
            if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
                loadData(event.getURL().toString());
        });
        add(new JScrollPane(display), BorderLayout.CENTER);
        setSize(500,300);
        setVisible(true);
    }

    //load the data to display on the screen
    private void loadData(String address){
        try{
            display.setPage(address);
            addressBar.setText(address);
        }catch(Exception e){
            System.out.println(e);
        }
    }
}

And here is the code in the Main class:

package WebBrowser;
import javax.swing.JFrame;
/**
 *
 * @author Siddharth Venu
 * 
 */
public class Main {
    public static void main(String[] args){
        ReadFile browser=new ReadFile();
        browser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

I then ran the Main class, at beginning, as it is not displaying any data from a website, it is looking good with address bar on the top. But when I enter an URL, say http://google.com, it displays weird output as in the following image. Display output of my browser Why exactly is this happening? The weird blue background and misaligned Google logo.

[Edit] I got to know that the setPage method can only handle HTML, plain text or RTF and not js. But it should at least display the HTML part without the weird blue screen na? PS: The blue screen is appearing in other sites like facebook too.

Upvotes: 1

Views: 827

Answers (1)

ZbyszekKr
ZbyszekKr

Reputation: 512

I've tested your browser. I'm getting the same results on pages with html5, javascript and css like google.com or facebook.com.

But when I use bare to the bones website like this it obviously works. So I must assume that the issue was the lack of support for these technologies in this simple browser.

As for the guy in the video linked he used bare html google webpage which I was unable to find or read out from the video (or made the video some time ago). People in the youtube comment section were addressing your issue as well. They were describing it as a lack of support for html5 in Swing. However some managed to make it work properly in JavaFX.

Upvotes: 1

Related Questions