Here you can find the source of newButtonPopup(final JToggleButton button)
public static JPopupMenu newButtonPopup(final JToggleButton button)
//package com.java2s; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPopupMenu; import javax.swing.JToggleButton; import javax.swing.SwingUtilities; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; public class Main { public static JPopupMenu newButtonPopup(final JToggleButton button) { final JPopupMenu popup = new JPopupMenu(); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { if (button.isSelected()) { popup.show(button, 0, button.getHeight()); } else { popup.setVisible(false); }/* w w w . ja v a 2 s . c o m*/ } }); popup.addPopupMenuListener(new PopupMenuListener() { public void popupMenuWillBecomeInvisible(PopupMenuEvent ev) { button.setSelected(false); // If this popup-hide was triggered by a mouse-press, then don't allow // that mouse-press to begin a click of the button (which would toggle // the popup back to visible again) // button.setEnabled(false); SwingUtilities.invokeLater(new Runnable() { public void run() { button.setEnabled(true); } }); } public void popupMenuWillBecomeVisible(PopupMenuEvent ev) { } public void popupMenuCanceled(PopupMenuEvent ev) { } }); return popup; } }