Java JButton Create newButtonPopup(final JToggleButton button)

Here you can find the source of newButtonPopup(final JToggleButton button)

Description

new Button Popup

License

Open Source License

Declaration

public static JPopupMenu newButtonPopup(final JToggleButton button) 

Method Source Code

//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;
    }
}

Related

  1. makeNavigationButton(String actionCommand, String toolTipText, String altText)
  2. makeRadioButton(String text, String actionCommand, ButtonGroup buttonGroup, boolean selected)
  3. makeToggleButton(final Object owner, final String id, final String altText, final int wpad, final int hpad)
  4. newButton(String name, String text)
  5. newButton(String text, ActionListener... listeners)
  6. newButtonsFrom(Collection action)
  7. newJButton()
  8. newJButton(String name, String tooltip, ActionListener al)