tags:

views:

122

answers:

3

If I create a set of actions to be used in a JFace application and I assign images to those actions, those images show up in both the toolbar (where I want them) and in the menus (where I don't want them).

Other than supplying two completely separate sets of actions (which eliminates part of the point of actions in the first place), how can I arrange to have those images displayed ONLY in the toolbar, and have the menus display only text?

A: 

I haven't checked this myself, but give this method a looksee:

public int getStyle() { ... }

It's defined in the Action class, and it appears to return the type of interface element that the Action is graphically represented as. So then you could override the getImageDescriptor() method:

public ImageDescriptor getImageDescriptor() {
    if (getStyle() == AS_DROP_DOWN_MENU)
        return null; // No icon in a menu
    return someImageDescriptor; // Actual icon
}
craig
Unfortunately, as far as I can tell, that won't help --- the same action is used for the toolbar and the menu so the getStyle value is the same in either case.
David
A: 

I ended up essentially duplicating the actions, making pairs, one with an image, one without, thereby eliminating the benefit of actions in the first place (sigh). On the other hand, since each action does just invoke a single method in the controller to perform the work, it's not too insidious and grouping the pairs together makes it reasonably clear what's going on.

David
+1  A: 

I ran into this problem as well (except I wanted different text for the toolbar and menu.) I ended up using the same action, but two different instances of it.

// Use this one in the menu
Action a = new Action();

// Use this one in the toolbar
Action a2 = new Action();
a2.setImageDescriptor(img);

Alternately, you could store the image descriptor in the action for the toolbar version and set it to null for the menu version.

thehiatus