Vadiraj's Blog

My experiments with Java

Getting downward arrow in the toolbar button with dropdown menu

Monday Dec 17, 2007

I wanted to create a toolbar button similar to debug or profiler button in NetBeans 6.0 IDE as in the image below.

 

This image shows the 'debug project' and 'profile project' with a small downward arrow that shows the user clearly that this is a button with a dropdown menu attached to it. To create a toolbar button with drop down menu, I followed the NetBeans DeveloperFAQ wiki which explains how to add dropdown to a toolbar button.

But the wiki help did not resulted in the purpose visually, although it helped me to put up a JToggle Button with attached drop down menu shown in the following images:

     

There's clearly no visual hint, unless clicked on the toolbar button. So I decided to look further into the source code. I failed to look up the 'debug project' toolbar button code (I couldn't find it in Java or Java/j2se modules), however, 'profile project' was easily visible. The class which creates this 'profile project' toolbar button is org.netbeans.modules.profiler.actions.ProfilerToolbarDropdownAction.java. The getToolbarPresenter() has an interesting code to create that small arrow..

JButton button = DropDownButtonFactory.createDropDownButton(
new ImageIcon(
new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)),
popup);
Actions.connect(button, defaultAction);

toolbarPresenter = button;

So instead of creating a JToggleButton, it creates a normal button with a predefined image as the button icon. I think, the Actions.connect() will combine the button with the current action (visually I guess). Its really nice and handy trick to get the visual hint. Following images show the result.

      

 

[1] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg