Hi, this technical blog will contain urls/tutorials that relate to latest technology. More so, I will also attempt to write tutes on my own to simplify things that are too complex. Thanks for your time in checking out this blog.

Tuesday, March 07, 2006

Two Minute Web Browser in Java

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.