Platinum Partner
java

Drop Down Buttons in Swing: A New Alternatives

The literature on Drop Down Buttons in Swing, as this search of Google reveals, is not extensive.

The literature on Drop Down Buttons in Swing, as this search of Google reveals, is not extensive. And what there is is dated. Here I give a brief overview of the existing resources and... provide a completely new and undocumented alternative that has existed since almost one year ago.

The best survey I have found on the status of Drop Down Buttons in Swing is The Ubiquitous Drop Down Button - Swing Style, on the Mammoth Software site. The author discusses the approach taken by Santhosh Kumar and another approach taken by the PSP (Personal Software Process) Dashboard. Problems across look and feels are identified and the consequences of extending the Box class instead of the JButton class are highlighted, among other areas. Then the Mammoth Software Drop Down Button is presented, requiring a certain amount of coding, because the solution is built from the ground up, which also has its advantages, such as the fact that you have a lot more control than if you were using someone's library.

However, let's look at a different approach. Irrespective of the question of which one is better/worse, it's at least useful knowing that this one is out there too. A NetBeans API changes log dating from May 4, 2007, says the following, in its typical lowkey tone of voice: "Added a factory class that can create special buttons with a small arrow icon that brings up a popup menu when clicked."

At least an exclamation mark would have been justified! Here, before I show the code, is the result in a simple sample application:

That's what one would expect a Drop Down Button to do, right? Well, there it is. In fact, it is one of the very many hidden features of the NetBeans Platform. But you can forget the NetBeans Platform (as well as NetBeans IDE). Both of these are irrelevant to this story, neither are needed, just one single JAR file from the NetBeans IDE distribution. In the same way as you can reuse the NetBeans Visual Library API in your own Java applications (i.e., outside of the NetBeans Platform), you can also take the NetBeans UI Utilities API (platform7/modules/org-openide-awt.jar in any distribution of NetBeans IDE) and simply make it available to your own application.

Once you've done that, life is good, you can use the org.openide.awt.DropDownButtonFactory class:

//declarations:private static JButton dropDownButton;    private static JPopupMenu popup;    private MyMenuItemListener menuItemListener;.........//constructor:public DemoJFrame() {    //create JFrame, add JMenuBar,    //together with some JMenus and JMenuItems:    initComponents();    //initialize JPopupMenu:    popup = new JPopupMenu();    //initialize ActionListener:    menuItemListener = new MyMenuItemListener();    //declare array of names:    String[] names = {"John", "Paul", "Ringo", "George"};    //iterate through the names,    //create JMenuItem from each,    //set name and add ActionListener,    //and then add to JPopupMenu:    for (int i = 0; i < names.length; i++) {        String name = names[i];        JMenuItem item = new JMenuItem(name);        item.setName(name);        item.addActionListener(menuItemListener);        popup.add(item);    }    //use the org.openide.awt.DropDownButtonFactory to create a DropDownButton,    //define the icon and then assign our JPopupMenu to the JButton:    dropDownButton = DropDownButtonFactory.createDropDownButton(            new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)),            popup);    //the JButton will be quite long by default,     //let's trim it by removing borders:    dropDownButton.setBorder(javax.swing.BorderFactory.            createEmptyBorder(1, 1, 1, 1));    //set the icon:    dropDownButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/demo/copy.gif")));     //add the JButton to the menu bar:    mainMenuBar.add(dropDownButton);}.........

I am curious to know what other Swing developers think of this solution. One thing I need to do is show the result under different look and feels, as the article mentioned in the introduction did. Other than that, it's great how this solution integrates so closely with existing JButtons and JMenuItems and simply repurposes them so effectively.

 

 

 

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}