Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Web Browser.....Code is inside...but clarify few things!

843810Nov 17 2005 — edited Mar 12 2007
HI All,
this is pilot ..sorry to bother u again but this time its kinda important
here is my program for a web browser--
now i wanna know a few things HyperlinkListener just not support javascript nor does it support flash ...thats 2 and i wanna know wht else restrictions that are in this simple browser....
and can u tell me how can i make this browser more faster and i wanna add a JProgressBar to it and i have no clue...please help/
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import java.io.IOException;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;
import javax.swing.JOptionPane;
 
 
public class Browser implements ActionListener
{
	JTextField t1;
	JLabel l1;
	JButton b1;
	GridBagLayout gbl;
	GridBagConstraints gbc;
	JPanel p;
	JFrame frame;
	JScrollPane scrollPane;
	JEditorPane jep;
	static String initialPage;
 
public Browser()
{
	//JFrame.setDefaultLookAndFeelDecorated(true);
	/*try
	{
	   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	}
	catch(Exception e)
	{
	    e.printStackTrace();
    }*/
	frame=new JFrame("Simple Web Browser");
 
	gbl=new GridBagLayout();
	gbc=new GridBagConstraints();
 
	p=new JPanel();
	p.setLayout(gbl);
 
	jep = new JEditorPane();
 
	t1=new JTextField();
 
	b1=new JButton("Check it out!");
	b1.addActionListener(this);
	frame.getRootPane().setDefaultButton(b1);
 
	l1=new JLabel("Address");
 
	gbc.anchor=GridBagConstraints.NORTHEAST;
	gbc.fill=GridBagConstraints.HORIZONTAL;
	gbc.gridx=0;
	gbc.gridy=0;
	gbc.weightx=0.0;
	gbl.setConstraints(l1,gbc);
	p.add(l1);
 
	gbc.anchor=GridBagConstraints.NORTHEAST;
	gbc.gridx=1;
	gbc.gridy=0;
	gbc.weightx=1.0;
	gbl.setConstraints(t1,gbc);
	p.add(t1);
 
	gbc.anchor=GridBagConstraints.NORTHWEST;
	gbc.gridx=2;
	gbc.gridy=0;
	gbc.weightx=0.0;
	gbl.setConstraints(b1,gbc);
	p.add(b1);
 
    scrollPane = new JScrollPane(jep);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    frame.getContentPane().add(p,BorderLayout.NORTH);
	frame.getContentPane().add(scrollPane);
    frame.setSize(1000,700);
    frame.setVisible(true);
}
 
public void actionPerformed(ActionEvent e)
{
		initialPage=t1.getText();
		char a=initialPage.charAt(0);
		char b=initialPage.charAt(1);
		char c=initialPage.charAt(2);
		char d=initialPage.charAt(3);
		char ea=initialPage.charAt(4);
		char f=initialPage.charAt(5);
		char g=initialPage.charAt(6);
 
		if((a=='h')&&(b=='t')&&(c=='t')&&(d=='p')&&(ea==':')&&(f=='/')&&(g=='/'))
		{
			initialPage=t1.getText();
		}
		else
		{
			initialPage="http://"+initialPage;
		}
		jep.setEditable(false);
		jep.addHyperlinkListener(new second(jep));
	    try
	    {
	      jep.setPage(initialPage);
	    }
	    catch (IOException ae)
	    {
		  JOptionPane.showMessageDialog(frame,new String("Error: "+ae));
	   }
}
public static void main(String[] args)
{
		new SimpleWebBrowser();
	}
}
class second implements HyperlinkListener
{
  private JEditorPane pane;
 
  public second(JEditorPane pane)
  {
    this.pane = pane;
  }
public void hyperlinkUpdate(HyperlinkEvent evt)
  {
   if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
   {
	 try
     {
       pane.setPage(evt.getURL());
     }
     catch (Exception e){}
  }}}

Comments

843810
HI All,
this is pilot ..sorry to bother u again
Hi. It's alright. That's what we're here for.
but this time its kinda important
ok. If it's that important you should go see your teacher. The scope ofthis question goes way beyond what we can provide here on these forums.
here is my program for a web browser--
now i wanna know a few things HyperlinkListener just not support >javascript nor does it support flash ...thats 2 and i wanna know wht >else
No, it doesn't support javascript or flash, as well as many other things that webpages have.

restrictions that are in this simple browser....

They aren't erally restrictions so much as things that don't work. In general:

JEditorPane is not built for nor can it serve as legitimate web browser. It's fine for displaying documentation etc that you've created using simple HTML. The way that it renders HTML is wrong more often than not, it's clunky and doesn't (at least to my knowledge) support stylesheets.

Other issues:

No support for form submissions.
When I was using it, it had not support for connecting to websites via SSL (ie https). This may have changed, or you may find an implementation on the web.

Plugins, different content types, and more.
and can u tell me how can i make this browser more faster and i
You're probably on your own on this one, but you could scan the web.
wanna add a JProgressBar to it and i have no clue...please help/
Then you need to learn.
843810
It took me about a week to make it and i guess it's going to take more time to add a JProgressBar to it coz i m not finding any method that can detect the current progress of the page........
ok. If it's that important you should go see your teacher. The scope of this question goes way beyond what we can provide here on these forums.
i think it's beyond her knowledge and in fact it's way beyond all the teachers whom i hav meet.

I think i need to learn more about how browsers operate and i also think that java does not provide enough classes or methods that can help anyone to make a browser......anywayz...

i tried to log in on yahoo and nothing happend coz of ssl
JEditorPane is not built for nor can it serve as ......
Does anyother component supports this kinda action---or provide better methods to overcome problems........
You're probably on your own on this one, but you could scan the web..
i tried to include only the neccessary classes to make the browser work fast ....r there any other things that can improve its efficiency

One more thing....... the main motivation behind this program was firefox i thought it was made in java.....and i m still not sure whether it is made in java......i thought if they can do it why can't i
anywayz u helped alot but i m still not sure on few points

The main being the little steps that go about when someone opens a page.....???????how does the browser go about it..
843810
I think I once saw an article in java.net about using the system default browser through java...

R. Hollenstein
843810
don't u think this program needs more discussion?
Herko_ter_Horst
One more thing....... the main motivation behind
this program was firefox i thought it was made in
java.....and i m still not sure whether it is made in
java......i thought if they can do it why can't
i
Firefox is not written in Java and it has taken a large number of people a large number of years to make it what it is today. While building a simple browser is a good learning excercise, I would let it go at that. The world does not need another broken browser ;)
843810
Broken Browser excuse me/...
check this out ---> run it and tell me if the code is broken...
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import java.io.IOException;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;
import javax.swing.JOptionPane;


public class Browser implements ActionListener
{
	JTextField t1;
	JLabel l1;
	JButton b1;
	GridBagLayout gbl;
	GridBagConstraints gbc;
	JPanel p;
	JFrame frame;
	JScrollPane scrollPane;
	JEditorPane jep;
	static String initialPage;

public Browser()
{
	//JFrame.setDefaultLookAndFeelDecorated(true);
	/*try
	{
	   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	}
	catch(Exception e)
	{
	    e.printStackTrace();
    }*/
	frame=new JFrame("Simple Web Browser");

	gbl=new GridBagLayout();
	gbc=new GridBagConstraints();

	p=new JPanel();
	p.setLayout(gbl);

	jep = new JEditorPane();

	t1=new JTextField();

	b1=new JButton("Check it out!");
	b1.addActionListener(this);
	frame.getRootPane().setDefaultButton(b1);

	l1=new JLabel("Address");

	gbc.anchor=GridBagConstraints.NORTHEAST;
	gbc.fill=GridBagConstraints.HORIZONTAL;
	gbc.gridx=0;
	gbc.gridy=0;
	gbc.weightx=0.0;
	gbl.setConstraints(l1,gbc);
	p.add(l1);

	gbc.anchor=GridBagConstraints.NORTHEAST;
	gbc.gridx=1;
	gbc.gridy=0;
	gbc.weightx=1.0;
	gbl.setConstraints(t1,gbc);
	p.add(t1);

	gbc.anchor=GridBagConstraints.NORTHWEST;
	gbc.gridx=2;
	gbc.gridy=0;
	gbc.weightx=0.0;
	gbl.setConstraints(b1,gbc);
	p.add(b1);

    scrollPane = new JScrollPane(jep);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(p,BorderLayout.NORTH);
	frame.getContentPane().add(scrollPane);
    frame.setSize(1000,700);
    frame.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
		initialPage=t1.getText();
		char a=initialPage.charAt(0);
		char b=initialPage.charAt(1);
		char c=initialPage.charAt(2);
		char d=initialPage.charAt(3);
		char ea=initialPage.charAt(4);
		char f=initialPage.charAt(5);
		char g=initialPage.charAt(6);

		if((a=='h')&&(b=='t')&&(c=='t')&&(d=='p')&&(ea==':')&&(f=='/')&&(g=='/'))
		{
			initialPage=t1.getText();
		}
		else
		{
			initialPage="http://"+initialPage;
		}
		jep.setEditable(false);
		jep.addHyperlinkListener(new second(jep));
	    try
	    {
	      jep.setPage(initialPage);
	    }
	    catch (IOException ae)
	    {
		  JOptionPane.showMessageDialog(frame,new String("Error: "+ae));
	   }
}
public static void main(String[] args)
{
		new Browser();
	}
}
class second implements HyperlinkListener
{
  private JEditorPane pane;

  public second(JEditorPane pane)
  {
    this.pane = pane;
  }
public void hyperlinkUpdate(HyperlinkEvent evt)
  {
   if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
   {
	 try
     {
       pane.setPage(evt.getURL());
     }
     catch (Exception e){}
  }}}
843810
Hi,
I think u can use another browser, instead of JEditorPane, now I amu sing Jdic browser which it's an IE control and had implemented some of events that can help u, so take a look at https://jdic.dev.java.net/
843810
http://landingthejob.blogspot.com
843810
It is possible to write a browser in Java. However, if you want to write a robust browser in Java you will have to start from scratch (not from predefined classes such as JEditorPane) and it will take alot of effort.

Try not to think of Java based on the API but on the language itself. The API is merely a set of predefined classes to be used to speed up implementation and standardize certain structures amongst other things.

DeltaCoder
843810
I almost forgot. You don't need to know the status of the html being displayed to show a status bar. Simply create a text status bar to display a message and another status bar similar to the status bar when windows is loading. The status bar will simply loop until complete (this doesn't give the user information as to how long it will take but does let the user know something is happening).
843810
Dear Friend,

I'm new to this swing browser concept ,while run u'r 'Browser'
example it will throw an exception like this
'Error:java.net.UnknowHostException:www.google.com'

Can u help me out plzzzzzzzzzzzzz........

(I know this is basic error but i don't know how to solve it )

Thanks in advance..
843810
Hi,
I think u can use another browser, instead of
JEditorPane, now I amu sing Jdic browser which it's
an IE control and had implemented some of events that
can help u, so take a look at
https://jdic.dev.java.net/
Not to sound "blasphemous", but what's the point in JDIC? The Browser class of org.jdesktop.jdic requires that you have an existing browser engine, which AFAIK only supports IE, to run a Browser.

So you need a browser to write a browser. Reinventing the wheel?

Phil
843810
Dear Friend,

I'm new to this swing browser concept ,while
run u'r 'Browser'
example it will throw an exception like this
'Error:java.net.UnknowHostException:www.google.com
/b]'

Can u help me out plzzzzzzzzzzzzz........

(I know this is basic error but i don't know how to
solve it )

Thanks in advance..

In case you were never able to resolve this, I had the same problem before, which is usually the result of a connectivity issue on your machine or network. Try flushing your connection, or, if worse comes to worst reboot and see what happens. Going forward, you'll want to put your JEditorPane.setPage() within a "try" block, handling java.net.UnknownHostException, java.net.RuntimeException and java.io.IOException

Phil
1 - 13
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 9 2007
Added on Nov 17 2005
13 comments
110 views