2012-10-15 44 views
0

我在Swing中有一個關於對話框,它使用JEditorPane來顯示html文檔(setContentType("text/html");)。
我想在Eclipse SWT應用程序中創建一個關於對話框,並顯示與Swing相同的html文檔,該文檔將顯示爲格式化(超鏈接,br等)。
我該怎麼做?什麼是合適的小部件? StyledText
我開始了在Browser一個Handler關於SWT應用程序中html頁面的對話框

@Execute 
    public void execute(final Shell currentShell){ 
     //load html file in textReader 
     Browser browser = new Browser(currentShell, SWT.NONE); 
     browser.setText(textReader.toString()); 
    } 

但不顯示任何內容。解決這個問題的正確方法是什麼?

更新:測試@Baz變化:

@Execute 
    public void execute(final Shell currentShell){ 
     currentShell.setLayout(new FillLayout()); 
     //load html file in textReader 
     Browser browser = new Browser(currentShell, SWT.NONE); 
     browser.setText(textReader.toString()); 
     currentShell.pack(); 
} 

它completelly遺址的應用程序!在HTML加載覆蓋現有元素

+0

這很難說,爲什麼不顯示任何東西只此代碼示例。你在「對話框」中顯示瀏覽器嗎? 'Shell'從哪裏來? StyledText目前無法使用HTML。 – Baz

+0

@Baz:我已經將一個'Handler'與一個用於在Application.e4xmi中定義的菜單項的命令關聯起來。當我按下按鈕時,處理程序即上面的'execute'方法被調用。我還需要其他哪些代碼爲此在這個帖子? – Cratylus

+0

對,對不起。剛剛意識到這是我還不知道的Eclipse4功能。在這裏我無法測試它,但也許嘗試在'Shell'上設置'FillLayout',或者在添加Browser後調用'pack()'。 – Baz

回答

1

您可以隨時使用好老的JFace Dialog與嵌入式Browser部件:

public class BrowserDialog extends Dialog { 

    private String browserString; 

    protected BrowserDialog(Shell parentShell, String browserString) { 
     super(parentShell); 
     this.browserString = browserString; 
    } 

    @Override 
    protected Control createDialogArea(Composite parent) { 
     Composite composite = (Composite) super.createDialogArea(parent); 

     GridLayout layout = new GridLayout(1, false); 
     composite.setLayout(layout); 

     GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 
     data.widthHint = 400; 
     data.heightHint = 400; 
     composite.setLayoutData(data); 


     Browser browser = new Browser(composite, SWT.NONE); 
     browser.setText(browserString); 
     browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

     return composite; 
    } 

    @Override 
    protected void createButtonsForButtonBar(Composite parent) { 
    } 

    @Override 
    protected void configureShell(Shell newShell) { 
     super.configureShell(newShell); 
     newShell.setText("About"); 
    } 

    @Override 
    public void okPressed() { 
     close(); 
    } 

    public static void main(String[] args) 
    { 
     final Display display = new Display(); 
     Shell shell = new Shell(display); 

     Color gray = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); 

     String hex = String.format("#%02x%02x%02x", gray.getRed(), gray.getGreen(), gray.getBlue()); 

     new BrowserDialog(shell, "<body bgcolor='" + hex + "'><h2>TEXT</h2></body>").open(); 
    } 

} 

是這樣的:

enter image description here

如果你想對話框按鈕,只需將createButtonsForButtonBar(Composite parent)方法替換爲:

@Override 
protected void createButtonsForButtonBar(Composite parent) { 
    createButton(parent, Dialog.OK, "OK", true); 
    createButton(parent, Dialog.CANCEL, "Cancel", false); 
} 

然後,它會是這樣的:

enter image description here

+0

'複合內容=新複合材料(currentShell);'不編譯 – Cratylus

+0

@Cratylus是的,對不起。錯字。見編輯的答案。 – Baz

+0

:無法使用 – Cratylus

相關問題