我想在我的非RCP SWT应用程序中添加自定义按钮标题。
MessageBox messageBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
messageBox.setText("Warning");
messageBox.setMessage("Save the changes before exiting?");
int buttonID = messageBox.open();
switch(buttonID) {
case SWT.YES:
// saves changes ...
case SWT.NO:
// exits here ...
break;
case SWT.CANCEL:
// does nothing ...
}
System.out.println(buttonID);
}
它工作正常,但我的按钮标题为“中止”,“重试”,“忽略”
我想要自定义按钮标题,例如“覆盖”,“重命名”,“取消”。
怎么做?
请帮忙。
*** 编辑 ********
我也试过
MessageDialog dialog = new MessageDialog(null, "Dangerous Activity", null,
"Are you sure you want to delete?", MessageDialog.CONFIRM,
new String[]{"Preview>", "Delete", "Cancel"}, 0)
{
protected void buttonPressed(int buttonId) {
setReturnCode(buttonId);
// close(); Call close for Delete or Cancel?
}};
但是MessageDialog要求应用程序为RCP,因此不导入所需的程序包。救命。
最佳答案
这是一个关于如何在SWT中执行自己的Dialog
的非常简单的示例(不过,使用JFace可以使用更舒适的方法):
public class CustomDialog extends Dialog
{
private String message = "";
private Shell shell;
public CustomDialog(Shell parent)
{
// Pass the default styles here
this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell = parent;
}
public CustomDialog(Shell parent, int style)
{
// Let users override the default styles
super(parent, style);
shell = parent;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public void open()
{
shell.setText(getText());
createContents(shell);
shell.pack();
shell.open();
Display display = getParent().getDisplay();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
}
/**
* Creates the dialog's contents
*
* @param shell
* the dialog window
*/
private void createContents(final Shell shell)
{
shell.setLayout(new GridLayout(3, true));
// Show the message
Label label = new Label(shell, SWT.NONE);
label.setText(message);
GridData data = new GridData();
data.horizontalSpan = 3;
label.setLayoutData(data);
// Display the input box
Label image = new Label(shell, SWT.NONE);
image.setImage(shell.getDisplay().getSystemImage(SWT.ICON_ERROR));
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
data.horizontalSpan = 3;
image.setLayoutData(data);
Button preview = new Button(shell, SWT.PUSH);
preview.setText("Preview");
data = new GridData(SWT.FILL, SWT.END, true, true);
preview.setLayoutData(data);
preview.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent event)
{
System.out.println("Preview");
}
});
Button delete = new Button(shell, SWT.PUSH);
delete.setText("Delete");
data = new GridData(SWT.FILL, SWT.END, true, true);
delete.setLayoutData(data);
delete.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent event)
{
System.out.println("Delete");
}
});
Button cancel = new Button(shell, SWT.PUSH);
cancel.setText("Cancel");
data = new GridData(SWT.FILL, SWT.END, true, true);
cancel.setLayoutData(data);
cancel.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent event)
{
shell.close();
}
});
shell.setDefaultButton(preview);
}
public static void main(String[] args)
{
CustomDialog dialog = new CustomDialog(new Shell());
dialog.setText("Title");
dialog.setMessage("Message");
dialog.open();
}
}
看起来像这样: