Hi! people, i have been hearing that a web browser in VB.NET takes 6 lines of code. How much lines of code do you think it would be in java??

Well, it takes just 3 lines of code. If you come back to me saying that it takes lot of pain to create the necessary GUI then “WTF, download netbeans”. With matisse, netbeans provides a solid foundation for designing GUIs, so that developers like me can forget GridBagLayout and rest in peace. Ok enough of that ;)

1. Open netbeans ide.
2. Create a new project. File->New Project. Select “General” and on the right pane, choose “Java Application”.
3. Click next. Give the project name as “TwoMinBrowser” and “Finish”.

Your project is ready. Go to java.net and download JDIC (JDesktop Integration Components). It doesn’t come as a part of Java SE 5.0 or J2SE 4.0. Good news is that, it is a part of Mustang (Java SE 6). Unpack the JDIC zip file and you will find a “jdic.jar”.


Click on the Projects tab in the IDE (Window->Projects), expand TwoMinBrowser. Now expand “Libraries” section, right click on it and select “Add JAR/Folder”. Navigate to the “jdic.jar” and add it. The projects tab should be like the image on the left.


Expand “Source Packages”, right click on “twominbrowser” and select “New->JFrame Form”. Give it a name and click “Finish”. From the Palette, drag n drop a JPanel. Resize it to fit the full form. Now add a JTextField (txtUrl), a JButton and JTabbedPane (browPane). The final form should be like the screenshot on the right. Click on the image to see a bigger one.

Double click on the JButton (”Go”) or right click on it and select Events->Action->actionPerformed. Write the following code. Press Alt+Shift+F to resolve unresolved classes. Press F5. Should you get any errors, delete the Main.java from the “Source Packages” node and press F5 again. It should now compile and execute.

try
{
WebBrowser wb=new WebBrowser();
wb.setURL(new URL(txtUrl.getText()));
browPane.add(wb);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,ex.getMessage());
}

Type in a proper url at the textbox and click on “Go”. Our TwoMinBrowser will work. This is the result of display of yahoo page in our browser. The same code will work in linux also.

Change the url and click on “Go” again, you should see a new tab. Thats it, ur tabbed browser is up and running in just 3 lines of code. If you are a netbeans user already, building this will take you only 2 minutes.

As i’m writing this, Netbeans 5.5 preview release is installing. It requires JDK 1.5.0_06. In case you don’t have, download it here. Read about the UML features of Netbeans 5.5 here and here.

Bye!

Update: Geertjan has a cool post on porting this simple application to netbeans platform.

5 Responses to “Two Minute Web Browser in Java”

  1. Arthanari Says:

    Hi,
    When i am trying to download this service its get error message..can u send me this jar file via mail..

  2. Sialyn Says:

    Hi, when I push de bottom buttom i get a pop-pup message saying no “protocol:url”. Do you know what is the problem?

  3. Sialyn Says:

    Sorry, I find de solution!! You have to write the complete URL with http. (i’m a fool)

    The programa give me another error, don’t show me the web and give this message: “Can’t execute native browser. ErrMsg is null”.

    Could you tell me what is the reason?

    Thank you!!!

  4. Aswin Anand Says:

    Sialyn, this problem could occur when the OS’ browser object couldn’t be embedded in the app. In windows, internet explorer works perfectly & firefox creates some problems regarding the same.

  5. Sialyn Says:

    Thank you!!

    I solved this problem when I downloaded it a more fresh jdic version.

    Now I have another problem when I use this browser with google maps (my own google-map application) it throws a runtime exception “GMap2 is not defined”. I read it and it’s a java script problema, probably.

    The ideas are wellcome!!

    Thank you

Leave a Reply