Java Jface对话框,如何正确检索用户按下的按钮?
我在Eclipse中使用自定义对话框时遇到问题 首先,我创建了一个扩展对话框的类Java Jface对话框,如何正确检索用户按下的按钮?,java,eclipse,jface,rcp,Java,Eclipse,Jface,Rcp,我在Eclipse中使用自定义对话框时遇到问题 首先,我创建了一个扩展对话框的类 public class ModificarGrupoBCDialog extends Dialog { private static final int CANCELAR = 999; private static final int MODIFICAR = 1; ... 我在某处创建了按钮 protected void createButtonsForButtonBa
public class ModificarGrupoBCDialog extends Dialog {
private static final int CANCELAR = 999;
private static final int MODIFICAR = 1;
...
我在某处创建了按钮
protected void createButtonsForButtonBar(Composite parent) {
this.createButton(parent, MODIFICAR, "Modificar", true);
this.getButton(MODIFICAR).setEnabled(puedeAltaGrupoBC());
this.bt_ok = this.getButton(MODIFICAR);
this.createButton(parent, CANCELAR, "Cancelar", false);
Display display = window.getShell().getDisplay();
Image image = new Image(display, ModificarGrupoBCDialog.class.getResourceAsStream("/icons/modificar.png"));
this.getButton(MODIFICAR).setImage(image);
image = new Image(display, ModificarGrupoBCDialog.class.getResourceAsStream("/icons/cancelar.png"));
this.getButton(CANCELAR).setImage(image);
}
当用户点击时
protected void buttonPressed(int buttonId) {
switch (buttonId) {
case MODIFICAR:
// Some Code, for Change Button
break;
case CANCELAR:
setReturnCode(CANCELAR);
close();
break;
}
最后,这就是我如何在调用者对象中打开并获取returnCode的方法
...
ModificarGrupoBCDialog modificarGrupoBC = new ModificarGrupoBCDialog(window.getShell(), window, gr_bc);
if (modificarGrupoBC.getReturnCode() == Window.OK) {
//... Some code on OK
} else {
//another code when cancel pressed.
}
;
如您所见,在尝试了一段时间后,我必须在CANCELAR开关块中编写setReturnCode(),可以吗?
我检查Dialog类是否自动指定了正确的返回代码
也许有人能给我指一个好的样品
我正在读Vogela的博客,可能解决方案是重写okPressed()方法
致以最诚挚的问候。标准对话框在两个位置设置返回代码:
protected void okPressed() {
setReturnCode(OK);
close();
}
protected void cancelPressed() {
setReturnCode(CANCEL);
close();
}
因此,您的代码需要:
setReturnCode(xxxx);
close();
只要您使用的按钮id与“取消”或“确定”按钮id不匹配,就可以
您还可以使用MessageDialog
所使用的方法,该方法只需执行以下操作:
protected void buttonPressed(int buttonId) {
setReturnCode(buttonId);
close();
}