Home > Java, NB Platform, NetBeans > Toolbar buttons with label

Toolbar buttons with label

As default, any action in a NetBeans toolbar is displayed with an icon only. If you want to show also a label for a desired action you have to implement a Presenter.TOOLBAR. If you don’t want do this for each action, register a custom org.openide.awt.Actions.ButtonActionConnector like this:

/**
 * @see org.openide.awt.Actions#connect(AbstractButton, Action)
 */
@ServiceProvider(service = ButtonActionConnector.class, position = 100)
public class MyButtonActionConnector implements ButtonActionConnector {

   @Override
   public boolean connect(AbstractButton button, Action action) {
      String text = (String)action.getValue("menuText"); // NOI18N
      if (text != null) {
         button.setAction(action);
         button.setText(Actions.cutAmpersand(text));
         String desc = (String)action.getValue(Action.SHORT_DESCRIPTION);
         if (desc != null) {
            button.setToolTipText(desc);
         } else {
            button.setToolTipText((String)action.getValue(Action.NAME));
         }
         return true;
      }
      return false;
   }

   @Override
   public boolean connect(JMenuItem item, Action action, boolean popup) {
      return false; // use default implementation
   }
}

If the property menuText is set, the the button is configured with icon and text. The basic use of the menuText Property is (copied from @ActionRegistration‘s javadoc):

Provides the JMenuItem text if one wants to use other than the name of the action returned by ActionRegistration.displayName().

With the custom ButtonActionConnector this property is “extended” to show a toolbar button text if supplied.

Provide a value for the @ActionRegistration’s menuText Attribute for the desired action(s):

@ActionRegistration(displayName = "#CTL_PwdChangeAction",
                    menuText = "#CTL_PwdChangeAction",
                    iconBase = "resources/key.png")
@ActionReference(path = "Toolbars/User")
@Messages("CTL_PwdChangeAction=Change Password…")
public final class PwdChangeAction implements ActionListener {
   ...
}

Result:

or

Tested with NetBeans 7.1.1 (see comments)

About these ads
  1. lanthaler
    2012-04-16 at 17:25

    How can I set the text below the icon ?

  2. 2012-04-16 at 20:44

    This is a basic Swing question:

    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.CENTER);
    
    • lanthale
      2012-04-17 at 10:45

      I have tried that and with NB 6.5 it was not working. Therefore I ended in replaceing the hole toolbar with a new component which was not so easy, but it is working now.

    • Rami
      2013-04-5 at 13:28

      I have tried that too and with NB 7.3 it was not working

  3. Rami
    2013-04-5 at 15:49

    Sorry, doesn’t work in mac but in windows ok

  1. 2012-04-19 at 16:15

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: