Mot
Mot

Reputation: 29530

SWT: how to create a plain push button from an action

I need a Button build from an IAction. Should I do that myself or is there already something in JFace which I can reuse? Note, I need the button instance, because I want to make it the default button in a dialog.

With new ActionContributionItem(action).fill(parent); I don't seem to be able to get the button instance.

Upvotes: 3

Views: 1719

Answers (2)

michal.kreuzman
michal.kreuzman

Reputation: 12390

I think that it's better way to use getWidget() method from ActionContributionItem to get Button instance associated with ActionContributionItem.

    ActionContributionItem aci = new ActionContributionItem(action);
    ai.fill(parent);
    Button widget = (Button) ai.getWidget();

Upvotes: 3

Mark Storer
Mark Storer

Reputation: 15868

After fill(parent) I think you can call parent.getChildren(). I expect the new button will be the last entry in the returned Control[]. Therefore:

Control kids[] = parent.getChildren();

if (kids != null && kids.length != 0) {
  getShell().setDefaultButton( (Button)kids[kids.length - 1] );

}

If it's not stuck on the end, you'll have to get the list list of children before and after, and find the new entry... but it'll almost certainly be tacked on the end.

Upvotes: 1

Related Questions