我正在创建一个包含一些信息的MessageDialog。
MessageDialog.openInformation(getShell(), "Success", "Change "+getNumber()+" has been created.");我希望能够复制对话框中的数字,这样我就可以将它粘贴到其他地方。有什么方法可以设置MessageDialog,这样我就可以完成这个任务了吗?
这个API可以找到这里。我没有在API中找到真正帮助我的任何东西。
发布于 2015-06-03 20:36:26
不,MessageDialog使用Label来显示消息。为了允许C&P,您需要一个Text小部件。因此,您必须创建自己的org.eclipse.jface.dialogs.Dialog子类。
您可以查看InputDialog的源代码作为示例。为了使文本小部件只读,使用SWT.READ_ONLY样式标志创建它。
发布于 2015-06-03 20:52:31
您可以创建从MessageDialog派生的类,并使用以下内容覆盖createMessageArea方法:
public class MessageDialogWithCopy extends MessageDialog
{
public MessageDialogWithCopy(Shell parentShell, String dialogTitle, Image dialogTitleImage,
String dialogMessage, int dialogImageType, String [] dialogButtonLabels, int defaultIndex)
{
super(parentShell, dialogTitle, dialogTitleImage, dialogMessage, dialogImageType,
dialogButtonLabels, defaultIndex);
}
@Override
protected Control createMessageArea(final Composite composite)
{
Image image = getImage();
if (image != null)
{
imageLabel = new Label(composite, SWT.NULL);
image.setBackground(imageLabel.getBackground());
imageLabel.setImage(image);
imageLabel.setLayoutData(new GridData(SWT.CENTER, SWT.BEGINNING, false, false));
}
// Use Text control for message to allow copy
if (message != null)
{
Text msg = new Text(composite, SWT.READ_ONLY | SWT.MULTI);
msg.setText(message);
GridData data = new GridData(SWT.FILL, SWT.TOP, true, false);
data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
msg.setLayoutData(data);
}
return composite;
}
public static void openInformation(Shell parent, String title, String message)
{
MessageDialogWithCopy dialog
= new MessageDialogWithCopy(parent, title, null, message, INFORMATION,
new String[] {IDialogConstants.OK_LABEL}, 0);
dialog.open();
}
}发布于 2015-06-03 20:37:54
只需使用JTextArea
然后
JTextArea tA= new JTextArea("your message.");
tA.setEditable(true);然后,可以添加
MessageDialog.openInformation(getShell(), "Success", "Change "+getNumber()+" has been created.");之后,通过稍微修改它(创建JTextArea,然后将它作为您的消息传递给JOptionPane )。
https://stackoverflow.com/questions/30630092
复制相似问题