A drop down button is a button with small arrow on its right side, and when the arrow is clicked, a popup appears with a list of possible actions. The user can either click an action from the list or click the button to activate the default action. Here’s a screenshot of a Swing program that illustrates the drop down button:

Swing Drop Down Button Demo

This is a useful component, but there is no such built-in component in Swing. Fortunately, there are some solutions out there which can fill this gap. In this tutorial, we introduce a pretty nice solution which depends on some external libraries (from NetBeans IDE) so you can write minimal implementation code without worrying much about the details. Now, let’s walk through the steps for creating a drop down button in Swing.

 

1. Add Required Libraries from NetBeans IDE

You have to add three JAR files which can be found under NetBeans’ installation directory, e.g c:\Program Files\NetBeans 8.0\platform\lib on Windows. Add the following JARs to your project:

  • org-openide-util.jar
  • org-openide-awt.jar
  • org-openide-util-lookup.jar 

2. Create Popup Menu

Next, create a popup menu which is shown up when the drop down button is clicked. The code is pretty simple and straightforward:

JPopupMenu popupMenu = new JPopupMenu();

JMenuItem menuItemCreateSpringProject = new JMenuItem("Spring Project");
popupMenu.add(menuItemCreateSpringProject);

JMenuItem menuItemCreateHibernateProject = new JMenuItem("Hibernate Project");
popupMenu.add(menuItemCreateHibernateProject);

JMenuItem menuItemCreateStrutsProject = new JMenuItem("Struts Project");
popupMenu.add(menuItemCreateStrutsProject);
Here, we add three menu items to the popup menu to represent a list of actions, e.g. creating a Spring project, Hibernate project or a Struts project.

 

3. Create Drop Down Button



Now, we come to the important part which uses the NetBeans IDE’s API to make a drop down button:

JPopupMenu popupMenu = createDropDownMenu(); // created above

ImageIcon icon = new ImageIcon(getClass().getResource("/net/codejava/swing/images/new.gif"));

JButton dropDownButton = DropDownButtonFactory.createDropDownButton(icon, popupMenu);
The key point here is that we use the DropDownButtonFactory class which has a factory method that creates a JButton from an ImageIcon and a JPopupMenu:

JButton dropDownButton = DropDownButtonFactory.createDropDownButton(icon, popupMenu);
The createDropDownButton() returns a trivial JButton which then can be added to a toolbar or other part of the program. For example:

JToolBar toolbar = new JToolBar();

// code add some buttons to the toolbar...

// add the drop down button
toolbar.add(dropDownButton);
 

4. A Swing Demo Program

For your convenience and reference, we create a working demo program which looks like this:

Swing Drop Down Button Demo

You can download full source code (including the required libraries of NetBeans) of this program in the Attachments section.

 

Related Swing Button Tutorials:

 

Other Java Swing Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (SwingDropDownButtonDemo.zip)SwingDropDownButtonDemo.zip[Java source code and libraries]637 kB

Add comment

   


Comments 

#5Kelemu2019-09-09 02:36
I need for more works
Quote
#4Nam2016-05-17 02:33
Quoting Ron:
I get error saying: org.eclipse.e4.core.di.InjectionException: java.lang.NoClassDefFoundError: org/openide/awt/DropDownButtonFactory


Maybe you forgot to add the org-openide-XXX.jar files.
Quote
#3Ron2016-05-17 01:22
I get error saying: org.eclipse.e4.core.di.InjectionException: java.lang.NoClassDefFoundError: org/openide/awt/DropDownButtonFactory
Quote
#2edjay abarientos2015-01-30 00:23
thank you for this!!! gonna use this for our project.keep sharing
Quote
#1saltydog6932015-01-15 07:54
Great Tutorial!!!
Quote