NetBeans Platform Cookbook Chapter 10 05

Use DropDownButton

Imagine you need a button which shows actual mode or tool and can switch its behaviour by popup menu.


Figure 10.8 Drop down button


Such a button can be created by DropDownButtonFactory class of the UI Utilities API module. The popup menu can be changed dynamically.

How to

Set dependency on UI Utilities module.

Create component where tabs will be used. Add new JButton or JToggleButton into it.

Open Properties of that button, switch to code and into CustomCreationCode insert creation code:

DropDownButtonFactory.createDropDownButton(MODES_BTN_ICON, modesMenu)
DropDownButtonFactory.createDropToggleDownButton(MODES_BTN_ICON, modesMenu)

Pass an Icon and a JPopupMenu into parameters. The menu can be later changed by calling

   button.putClientProperty( PROP_DROP_DOWN_MENU, otherMenu );

The used menu action can set its action to the button:

   public void actionPerformed(ActionEvent e) {
        setTool( this.tool );
        if (toolsButton.getAction() != this)
            toolsButton.setAction( this );
        do other work

Notes/Tips - documentation

Documentation links:

DropDownButtonFactory class

UI Utilities API .

Notes/Tips - examples

Try example sources in 10_best / DropDownButton directory.

Text and sources were created under NB 6.8, 6.9, 7.0 and 7.1.


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo