views:

2902

answers:

2

In Eclipse, its easy to specify buttons for your toolbar using the ActionSets extension point. However, when I need to specify some items programmatically, I can't get the same look. I don't believe that the framework is using native buttons for these, but so far, I can't find the right recipe to match the Eclipse look. I wanted to see if anyone has found the right snippet to duplicate this functionality in code.

+2  A: 

Could you perhaps put in an extract of the code you have for adding actions programmatically to the toolbar? I assume you do this in an ApplicationActionBarAdvisor class? Their should be no difference in the look of buttons you add declaratively vs those you add programatically.

Herman Lintvelt
+3  A: 

It's difficult to tell from your question, but it sounds like you may be attempting to add a ControlContribution to the toolbar and returning a Button. This would make the button on the toolbar appear like a native button like you seem to be describing. This would look something like this:

IToolBarManager toolBarManager = actionBars.getToolBarManager();
toolBarManager.add(new ControlContribution("Toggle Chart") {
    @Override
    protected Control createControl(Composite parent)
    {
        Button button = new Button(parent, SWT.PUSH);
        button.addSelectionListener(new SelectionAdapter() {
        @Override
     public void widgetSelected(SelectionEvent e) {
                // Perform action
            }
        });
     }
});

Instead you should add an Action to the toolbar. This will create a button on the toolbar that matches the standard eclipse toolbar buttons. This would look something like this:

Action myAction = new Action("", imageDesc) {
    @Override
    public void run() {
        // Perform action
    }
};

IToolBarManager toolBarManager = actionBars.getToolBarManager();
toolBarManager.add(myAction);
hudsonb