RSS

Building the worst browser in Java

28 Jun

This is gonna be the worst browser you’ve ever seen. Java has a very poor support for Css/Javascript. But if you are trying to load a page in the web written a decade ago, then you just struck the jackpot. Anyway I’ll use this later on to show how other swing objects can be used.

and if you wanna try it out and improve it:

package wordpress.mycodeandlife.com;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class WebBrowser extends JFrame implements ActionListener, HyperlinkListener{
	JTextField addressbar;
	JEditorPane page;
	JLabel status;

	WebBrowser(){
		super("gilo");
		addressbar = new JTextField();
		addressbar.addActionListener(this);

		page = new JEditorPane();
		page.setEditable(false);
		page.addHyperlinkListener(this);

		status = new JLabel();
		status.setText("Welcome to gilo");

		add(addressbar,BorderLayout.NORTH);
		add(status, BorderLayout.SOUTH);
		add(new JScrollPane(page),BorderLayout.CENTER);

		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500,500);

		//These three are the proxy settings
		System.getProperties().put("proxySet","true");
		System.getProperties().put("proxyPort","3128");
		System.getProperties().put("proxyHost","10.223.0.2");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		loadpage(e.getActionCommand());

	}

	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
			loadpage(e.getURL().toString());
		else if(e.getEventType()==HyperlinkEvent.EventType.ENTERED)
			status.setText(e.getURL().toString());
		else if(e.getEventType() == HyperlinkEvent.EventType.EXITED)
			status.setText(" ");

	}

	public void loadpage(String url){

		status.setText("Loading "+ url + "....");
		try {

			page.setPage(url);
			addressbar.setText(url);
			this.setTitle(url);
			status.setText(url);

		} catch (IOException e) {
			status.setText("Error unable to connect to: " + url);
		}
	}

	public static void main(String args[]){

		WebBrowser brow = new WebBrowser();

	}

}

Advertisements
 
Leave a comment

Posted by on June 28, 2012 in Code, Java

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: