Saturday February 24, 2007
JDIC Browser on the NetBeans Platform
A completely unexpected thing that came out of NetBeans Day, for me, is that I now have my own JDIC browser integrated in NetBeans IDE. In fact, I am typing this blog inside a TopComponent. All the way back on November 1, 2005, I was already able to do this (and was pretty excited about it). But, in that case, I used an implementation of JDIC by Rich Unger. Now, however, I've done it myself. It all started yesterday when I was going through the list of names and e-mails that I had gathered from various people I had met at Sun Tech Days Hyderabad. One of them, who I briefly talked to after NetBeans Day, was from Aswin. He had left me the URL to his blog. So I went there and browsed around a bit. And guess what I found? A very cool blog entry by Aswin called Two Minute Web Browser in Java. In this really cool blog entry, Aswin shows how easy it is to create a JDIC browser in a Java application. It was really tempting to... begin thinking about porting his application to the NetBeans Platform. And so I did.
First, I built the application he described. Really cool, nice, clear and wonderful. It required virtually no coding. However, I encountered problems that many others had before me. For example, I ended up using the IE version of the embedded browser, which has problems with scrollbars, as you can see here... no horizontal scrollbar to be seen:

However, it wasn't long before I had done a 'simple port'. In other words, I moved the code into a NetBeans API TopComponent, and put the jdic.jar and native libraries in a library wrapper module. I also added 'Back', 'Forward', and 'Refresh' buttons, which were easy to program because of methods provided by JDIC. Here's the result, which I was already happy with, despite its limitations:

Note that I put the text field and buttons below the returned web page, instead of the normal place, which is above it. Why? Because of the lightweight/heavyweight component problem, which resulted in the drop-down list not rendering correctly, being hidden behind the returned web page. I looked around for solutions, and have found some, but just haven't implemented one yet. In the meantime, I moved the controls to the bottom of the browser, so that there wouldn't be a problem using the browser.
The next problem was that I was using JTabbedPanes, just like Aswin's original. However, there is no way of closing an individual tab. Normally, one would expect one of those little X symbols, which need to be clicked to close a tab. Both Joerg Plewe, who is a NetBeans Dream Team member, and Kirill Grouchnikov have blogged extensively about this issue. I initially adopted Kirill's approach, which meant I had to install the Substance look and feel. This was the result:

I was pretty happy with that, at least now I could close the tabs. However, I had reached the limitations of my 'simple port'. Everything was working in my TopComponent, but I hadn't leveraged anything else of the NetBeans Platform. I then began using more of what the NetBeans Platform has to offer, spreading out the ui more logically across the 'real estate' of the IDE. So, now, as a result of these additional steps, a new instance of a TopComponent opens, whenever I press enter in the text field afer entering a valid URL. And, good news, each instance of the TopComponent has its own X sign, as well as many other out of the box features, so this is a much better option than sticking with the JTabbedPane. However, within the TopComponent I still have a JTabbedPane. That seems to be the only container that can display a JDIC web browser. It is very odd. So now I have two tabs per web page, one from the JTabbedPane and one from the TopComponent. Not ideal, but still better than using only JTabbedPanes. So here's the result, with this blog entry used in the screenshot:

Note that here the controls (i.e., the text field and buttons) are in the toolbar, instead of being part of the TopComponent. And what you can't see here is that the status bar gives the user messages when the URL is invalid. In other words, this porting procedure has gone beyond the 'simple' TopComponent port, to a more complete transfer. Still far from complete, but clearly a functioning browser that is already 100 times better than the Swing browser that is bundled with the IDE. Notice that it is completely integrated in the IDE, so that, for example, you can flip from one browser page to a Java class, just like any other window in NetBeans IDE, for which no coding was necessary on my part:

Plus, it can be installed in any application built on the NetBeans Platform (and there can't be many that wouldn't benefit from a high quality web browser), although currently only on Windows operating systems. And it all came out of coming across Aswin's cool instructions for creating a small browser, out of a few lines of code. Thanks Aswin!
Feb 24 2007, 03:36:23 AM PST Permalink
Posted by Tom Corbin on February 24, 2007 at 06:11 AM PST #
2 - For "Because of the lightweight/heavyweight component problem, which resulted in the drop-down list not rendering correctly, being hidden behind the returned web page." have you tried:
http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#setLightWeightPopupEnabled(boolean)
3 - The JDIC Browser should/could be wrapped as a
http://www.netbeans.org/download/dev/javadoc/org-openide-awt/org/openide/awt/HtmlBrowser.html
Posted by Geertjan on February 24, 2007 at 12:24 PM PST #
Posted by Rich Unger on February 25, 2007 at 01:45 AM PST #
Posted by Geertjan on February 25, 2007 at 05:30 AM PST #
Posted by Geertjan on February 25, 2007 at 05:31 AM PST #
Posted by Gustavo on February 25, 2007 at 09:51 AM PST #
Posted by Geertjan on February 25, 2007 at 10:02 AM PST #
Posted by Rich Unger on February 25, 2007 at 10:08 AM PST #
Posted by Aswin Anand on February 26, 2007 at 06:00 AM PST #
Posted by Geertjan on February 26, 2007 at 07:25 PM PST #
Posted by Alex on April 02, 2007 at 08:36 PM PDT #
Posted by Geertjan on April 03, 2007 at 02:52 AM PDT #
Posted by Geertjan on April 03, 2007 at 02:54 AM PDT #


