Below code shows how to create a JToolBar with a drop down using JPopupMenu First, before starting coding this project you need to a...
Below code shows how to create a JToolBar with a drop down using JPopupMenu
First, before starting coding this project you need to add the following Jar files into your project in Netbeans.Find them here c:\Program Files\NetBeans 8.0\platform\lib on Windows
Steps .
1. Add below jar files to your project
First, before starting coding this project you need to add the following Jar files into your project in Netbeans.Find them here c:\Program Files\NetBeans 8.0\platform\lib on Windows
Steps .
1. Add below jar files to your project
- org-openide-util.jar
- org-openide-awt.jar
- org-openide-util-lookup.jar
2.Create popupmenu
JPopupMenu popupMenu = new JPopupMenu(); JMenuItem sales = new JMenuItem("Sales Report"); popupMenu.add(sales); JMenuItem customer = new JMenuItem("Customers"); popupMenu.add(customer); JMenuItem items = new JMenuItem("Items"); popupMenu.add(items);
3.Create DropDown Button
ImageIcon icon = new ImageIcon(getClass().getResource("/icons/report.png")); JButton dropDownButton = DropDownButtonFactory.createDropDownButton(icon, popupMenu);4.Create JToolBar
ImageIcon icon = new ImageIcon(getClass().getResource("/icons/report.png")); JButton dropDownButton = DropDownButtonFactory.createDropDownButton(icon, popupMenu);Complete Source code.
import java.awt.HeadlessException; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JToolBar; import javax.swing.JPopupMenu; import javax.swing.ImageIcon; import javax.swing.JMenuItem; import org.openide.awt.DropDownButtonFactory; /** * * @author Eric * Site:www.techoverload.net */ public class Download extends JFrame { Download() throws HeadlessException{ JPopupMenu popupMenu = new JPopupMenu(); JMenuItem sales = new JMenuItem("Sales Report"); popupMenu.add(sales); JMenuItem customer = new JMenuItem("Customers"); popupMenu.add(customer); JMenuItem items = new JMenuItem("Items"); popupMenu.add(items); ImageIcon icon = new ImageIcon(getClass().getResource("/icons/report.png")); JButton dropDownButton = DropDownButtonFactory.createDropDownButton(icon, popupMenu); JToolBar toolbar = new JToolBar(); toolbar.add(dropDownButton); add(toolbar); setSize(200,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new Download(); } }