Two Minute Web Browser in Java

07Mar06

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!

Advertisements


4 Responses to “Two Minute Web Browser in Java”

  1. Well ur Explanation for creating a browser is enough for the complexity involved in creating the Browser in Java.
    And man, look at the UI….S**t man WTF was that. Look at the Windows GUI and the one ur Java App has created. Can u create the Presntation Graphics of VB.NET web browser with ur 3 line code. I think the extra 3 line in VB.NET give u more cooler looks than the Netbeans.

    IT IS THE CHOICE THAT CAUSES THE PROBLEM.

  2. with 1 extra line of code, u can get the windows look and feel

  3. 3 rekha

    woww its very nice to have this info.abhaaa writing codes n java s 22 much now dats done easy ah…cool!!!!

  4. @rekha:
    use both netbeans & VS so that u know the improvements on both sides of the world–>


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: