Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ How do I display a Web page in SWT?"

 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
 
it can be used for highly controlled applications, such as displaying help text or even
 
it can be used for highly controlled applications, such as displaying help text or even
 
for showing decorated and interactive text inside a view or an editor.
 
for showing decorated and interactive text inside a view or an editor.
 
 
 
  
 
The browser has API for programmatically manipulating the content, such as
 
The browser has API for programmatically manipulating the content, such as
Line 19: Line 16:
 
take a look at <tt>BrowserAction</tt> in the <tt>org.eclipse.faq.examples</tt> plug-in.
 
take a look at <tt>BrowserAction</tt> in the <tt>org.eclipse.faq.examples</tt> plug-in.
 
This action implements a fully functional Web browser in fewer than 60 lines of code!
 
This action implements a fully functional Web browser in fewer than 60 lines of code!
 
  
  
Line 47: Line 43:
 
         display.sleep();
 
         display.sleep();
 
</pre>
 
</pre>
 
 
  
  
Line 68: Line 62:
 
&nbsp;&nbsp;&nbsp;&nbsp;'''Figure 7.1'''&nbsp;&nbsp;
 
&nbsp;&nbsp;&nbsp;&nbsp;'''Figure 7.1'''&nbsp;&nbsp;
 
SWT browser widget
 
SWT browser widget
 +
 +
See also [[How can I invoke the eclipse default web browser in my own plugin?]].
  
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>

Revision as of 18:28, 24 November 2006

In Eclipse 3.0, SWT introduced a browser widget for displaying a native HTML renderer inside an SWT control. Prior to the introduction of this browser, it was necessary to invoke an external Web browser program for displaying rendered HTML. The browser can be instructed to render either a URL or a supplied string containing HTML content. The browser widget does not include the usual controls for navigation, bookmarks, and all the usual bells and whistles associated with a Web browser. As such, it can be used for highly controlled applications, such as displaying help text or even for showing decorated and interactive text inside a view or an editor.

The browser has API for programmatically manipulating the content, such as browsing forward or back in the navigation history, refreshing the content, or halting a rendering in process. You can install listeners on the browser to be notified when the location is changing or when the title changes or to receive progress notification as a page loads. It is fairly straightforward to implement basic Web browser functionality around this browser widget. For more details, take a look at BrowserAction in the org.eclipse.faq.examples plug-in. This action implements a fully functional Web browser in fewer than 60 lines of code!


For a richer example, look at the org.eclipse.ui.examples.browser project in the Eclipse repository (dev.eclipse.org). This project implements a Web browser as a stand-alone Eclipse RCP application. As a quick example, here is a stand-alone SWT snippet that opens a browser shell on this book&#146;s Web site.

A title listener is added to the browser in order to update the shell title with the name of the Web page being displayed:

   Display display = new Display();
   final Shell shell = new Shell(display, SWT.SHELL_TRIM);
   shell.setLayout(new FillLayout());
   Browser browser = new Browser(shell, SWT.NONE);
   browser.addTitleListener(new TitleListener() {
      public void changed(TitleEvent event) {
         shell.setText(event.title);
      }
   });
   browser.setBounds(0,0,600,400);
   shell.pack();
   shell.open();
   browser.setUrl("http://eclipsefaq.org");
   while (!shell.isDisposed())
      if (!display.readAndDispatch())
         display.sleep();


Figure 7.1 shows the resulting browser inside a simple shell. The browser widget is not yet available on all platforms as not all platforms that SWT supports have an appropriate native control that can be exploited. For Eclipse 3.0, the browser will at least be available on Windows, Linux, QNX, and MacOS. For platforms that do not have a browser widget available, the Browser constructor will throw an SWT error, allowing you to catch the condition and fall back to an alternative, such as a user-specified external browser.



    <img src=../images/browser_snippet.png>


    Figure 7.1   SWT browser widget

See also How can I invoke the eclipse default web browser in my own plugin?.


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.