2013-09-28 85 views
2

我正在項目中實現SWT瀏覽器以顯示HTML 頁面。當用戶在該瀏覽器上點擊右鍵時,會顯示一個彈出式菜單,其功能如「打印」,「打印視圖」。如果我將它們放在工具欄上,可以使用單獨的按鈕來完成此操作嗎?SWT瀏覽器控件右鍵單擊彈出菜單調用按鈕的功能單擊

enter image description here

瀏覽器控制的另一個功能是使用 「CTRL + F」,這帶來了發現對話。這個對話可以用按鈕調用嗎?

enter image description here

幫我嗎?

回答

2

當按下Button時,您可以通過執行JavaScript來實現其中一些目標。

下面是打印對話框的例子:

public static void main(String[] args) 
{ 
    Display display = Display.getDefault(); 
    final Shell shell = new Shell(display); 
    shell.setText("StackOverflow"); 
    shell.setLayout(new GridLayout(1, false)); 

    final Browser browser = new Browser(shell, SWT.NONE); 
    browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

    Button button = new Button(shell, SWT.PUSH); 
    button.setText("Search"); 

    button.addListener(SWT.Selection, new Listener() 
    { 
     @Override 
     public void handleEvent(Event arg0) 
     { 
      browser.execute("window.print()"); 
     } 
    }); 

    shell.pack(); 
    shell.setSize(600, 400); 
    shell.open(); 
    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 

只是在網上搜索如何實現你想要使用JavaScript來添加其他功能。

+0

感謝巴茲,但我想要一個瀏覽器的打印預覽。這個令人吃驚的使用javascript – Abhit

+0

@Abhit http://stackoverflow.com/questions/230205/how-can-print-preview-be-called-from-javascript – Baz

相關問題