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.
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();
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.
精彩评论