2015-06-03 59 views
1

我正在用一些信息創建MessageDialog。複製並粘貼MessageDialog消息

MessageDialog.openInformation(getShell(), "Success", "Change "+getNumber()+" has been created."); 

我希望能夠複製對話框中的號碼,所以我可以將其粘貼在其他地方。有沒有辦法設置MessageDialog,所以我可以做到這一點?

該API可以找到here。我在API中沒有找到真正幫助我的東西。

回答

2

不,MessageDialog使用Label來顯示消息。爲了允許C & P,您需要改爲Text小部件。所以你必須創建你自己的子類org.eclipse.jface.dialogs.Dialog

作爲示例,您可以查看InputDialog的源代碼。爲了使文本小部件爲只讀,請使用SWT.READ_ONLY樣式標誌創建它。

+0

無賴。我會研究這一點。謝謝! –

0

只需使用一個JTextArea

然後

JTextArea tA= new JTextArea("your message."); 
tA.setEditable(true); 

那麼你可以後添加

MessageDialog.openInformation(getShell(), "Success", "Change "+getNumber()+" has been created."); 

,通過改變它一點點(你創建的JTextArea,然後傳遞以JOptionPane作爲您的消息。)

+1

這個問題被標記爲eclipse,所以它是關於SWT,而不是Swing。 – ralfstx

+2

哦,那我的不好。 – CCV

0

您可以創建一個從派生的類並覆蓋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(); 
    } 
} 
+0

我對這應該如何工作有點困惑。我沒有一個Composite來傳入這個方法。我想我可以創建一個。那我會用複合材料來打開這些信息嗎? –

+0

您可以創建一個從MessageDialog派生的類,並在該類中重寫'createMessageArea'方法。 –

+0

增加了更多的類 –