java embedded browser plugin

There are four kinds of plug-ins used for Java embedded browsers on the Internet. Let me introduce them to you below:

1. Implementation in JDK - JEditorPane
Swing is a toolkit for developing graphical user interface for Java applications. It is based on the Abstract Window Toolkit (AWT) so that cross-platform applications can use any pluggable appearance. Style, and it is a lightweight (light-weight) component, no native code, does not depend on operating system support, this is the biggest difference between it and AWT components.

In Swing, one component is the JEditorPane, which is a text component that can edit arbitrary content. This class uses EditorKit for its operations, which effectively converts its type to the appropriate text editor kind for whatever content is given to it. The editor's content type at any given time is determined by the currently installed EditorKit.

By default, JEditorPane supports the following content types:
text/plain
plain text content, the toolkit used in this case is an extension of DefaultEditorKit that generates plain text views with wrapping.
text/html
HTML text, the toolkit used in this case is javax.swing.text.html.HTMLEditorKit, which supports HTML3.2.
text/rtf
RTF text, the toolkit used in this case is the class javax.swing.text.rtf.RTFEditorKit, which provides limited support for Rich Text Format.

Common methods of
JEditorPane JEditorPane()
Creates a new JEditorPane object
JEditorPane(String url)
Creates a JEditorPane from a string containing a URL specification
JEditorPane(String type, String text)
Creates a JEditorPane that has been initialized to a given file
JEditorPane(URL initialPage )
Create a JEditorPane based on the specified URL
scrollToReference(String reference)
scrolls the view to the given reference position (that is, the value returned by the URL.getRef method of the URL being displayed)
setContentType(String type)
Set this editor The type of content to be processed
setEditorKit(EditorKit kit)
sets the toolkit currently installed for processing the content
setPage(String url)
sets the URL to be displayed currently, the parameter is a String
setPage(URL page)
sets the URL to be displayed currently, the parameter is A java.NET.URL object
setText(String t)
Sets the text of this TextComponent to the specified content, which is expected to be provided in this editor's content-type format JEditorPane
Displays
a
page
);
set the content displayed by JEditorPane to text/html
jep.setContentType("text/html");
set it to be
uneditable jep.setEditable(false);
handle hyperlink events
jep.addHyperlinkListener(this);
JEditorPane needs to register a HyperlinkListener object To handle hyperlink events, this interface defines a method hyperlinkUpdate(HyperlinkEvent e), the sample code is as follows:

public void hyperlinkUpdate(HyperlinkEvent event) 
{ 
    if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 
    { 
        try 
        { 
            jep.setPage(event.getURL()); 
        } 
        catch(IOException ioe) 
        { 
            ioe.printStackTrace(); 
        } 
    }        
} 

The complete code can be downloaded in this article. In this example, a HyperlinkListener interface is implemented, and in the method implementation,

if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED)

This line of code means to first judge the type of HyperlinkListener, and only process events whose event type is HyperlinkEvent.EventType.ACTIVATED (ie, click on a hyperlink event), and then get the hyperlink by calling the getURL() method of HyperlinkEvent. URL address.

Finally, JEditorPane displays the new URL address by calling the jep.setPage(event.getURL()) method.

Advantages and disadvantages of JEditorPane
Since JEditorPane is included in Swing in J2SE, there is no need to import third-party jar files, which is relatively simple. However, the JEditorPane class has weak support for the display processing of CSS in web pages and the execution of JavaScript scripts, and the official does not seem to have any plans to improve the JEditorPane class. If you want to use JEditorPane to display common URLs, you will find the displayed page. It is very different from IE and Firefox, and cannot handle page logic normally. So if it is only used to display relatively simple HTML, JEditorPane is still a good choice.

Second, the open source Java Web browser implementation - Lobo

The Lobo project is a third-party open source Java browser project whose official website is http://lobobrowser.org/java-browser.jsp . It is implemented using 100% Java code and has full HTML4, javascript and CSS2 support, in addition to direct JavaFX rendering.

Lobo itself is already a complete browser software, and it also provides many APIs related to web browsers for Java programmers to use or extend in their own code, including rendering engine API, browser API and Plugin system API and more. Specific functions can be viewed on its official website.

FramePanel in Lobo

The class that implements the Web browser in Lobo is called FramePanel, which provides the encapsulation of the HTML page display and some auxiliary methods. Here's an introduction to some common methods:
FramePanel()
creates a single FramePanel object that can be added to any Swing window or component.
boolean back()
the browser returns to the previous page
boolean alert(String message)
opens a prompt dialog with a message
boolean forward()
the browser goes to the next interface
boolean confirm(String message)
opens a confirmation dialog showing a specific Message
void navigate(String url)
displays a specific URL in this component, parameter a URL String
void reload()
reloads the current interface
addContentListener(ContentListener listener)
adds a content change listener
addNavigationListener(NavigationListener listener)
adds a navigation (The displayed URL changes) the listener
addResponseListener(ResponseListener listener)
adds a listener that gets the result returned by the server

Use FramePanel

To use FramePanel, you first need to download its installation package on its official website, and then in the installation directory, you can see lobo.jar and lobo-pub.jar, and add these two jar files to the classpath. Similar to JEditorPane, to use FramePanel have the following steps:

创建一个 FramePanel 的对象
FramePanel browser=new FramePanel(); 
将这个对象添加到界面上。Frame 是继承自 JPanel, 所以可以像其他 Swing 组件一样地添加到 JPanel 或者窗口上面。
通过调用 FramePanel.navigate(url) 的方法来设置要显示的网址。
不同于 JEditorPane,FramePanel 已经默认处理了点击超链接的事件,不需要另外手动地编写代码来处理。

Pros and Cons of Lobo Browser API

First of all, Lobo's FramePanel is a 100% pure Java implementation, which has good portability and can run normally under Window/Linux platforms. Compared with JEditorPane in Swing, it has greatly improved the display of HTML and CSS and the execution of JavaScript. After the actual use test, when visiting most web pages, it can be displayed normally, which is similar to the mainstream IE/Firefox, but its support for CSS is not very complete. For some more complex web pages, the display Access is relatively large. In addition, the documentation of the Lobo project is not particularly complete, which may be a reason for limiting its widespread use.

3. Browser components in JDICplus

The background of the JDIC (Java Desktop Integration Components) project is to allow Java programs to be seamlessly integrated with the system when platform portability of Java code can be ignored. It provides Java code with direct access to the local desktop, including a series of Java packages and tools, including the ability to embed native browser components, start desktop applications, add tray icons to the desktop's system tray, and register files Type associations, etc.

JDICplus is another extension on the JDIC project. It is an extension development kit for the win32 operating system of Java, that is to say, it can only be used on the Windows operating system, so it is not platform independent. It provides many functions similar to Windows API, in addition to the encapsulation of IE components, there are also map display components, and components for editing and browsing MS Word, MS PPT, MS Excel, MS Outlook, and PDF. The official website of JDICplus is https://jdic.dev.java.net/documentation/incubator/JDICplus/index.html . This page shows a lot of DEMOs that use the JDICplus library. The discussion here is mainly about the encapsulation of Windows IE browser component.

Browser component using JDICplus

To use JDICplus, the first thing you need is the configuration of the environment. JDICplus requires JDK6.0 or higher, you need to download the latest version from www.sun.com first. Otherwise, the code will not compile and run normally.

The second is to download the JDICplus class library and add it to the classpath.

The component encapsulated by IE in JDICplus is org.jdic.web.BrTabbed, which not only encapsulates the display of web pages, but also handles the functions of multiple tab pages, so it is relatively powerful. The steps to use it are as follows:
Initialize the JDICplus component and set it to non-design mode. The default mode of this component is design mode. If it is used to display HTML content at this time, there will be a lot of useless information. The code is as follows:
org.jdic.web.BrComponent.DESIGN_MODE = false;
Initialize a BrTabbed component:
private BrTabbed browser=new BrTabbed();
Add to the interface, BrTabbed is a subclass of JPanel, so it can be used directly in Swing, just Add to a JPanel or window like any other Swing component.

The BrTabbed class already handles the event of clicking on a hyperlink, and also supports multiple tabs, which do not require manual coding to handle it.

Advantages and disadvantages
of JDICplus browser component The browser component in JDICplus uses JNI to encapsulate IE, so its display effect is exactly the same as IE (including the right-click menu), and BrTabbed also has built-in multi-tab function , is relatively simple to use, and also does not need to deal with the event of clicking a hyperlink. Its disadvantage is that it must use JDK6.0 or above, and the requirements are relatively high. At the same time, it uses the API related to the Windows operating system at the bottom, so it is not platform independent.

Fourth, the browser component in
SWT SWT (The Standard Widget Kit) is a set of open source component library of Java, which provides an efficient ability to create a graphical user interface, and is also one of the UI components of the Eclipse platform. Compared with Swing, it is relatively fast, and because it uses the same rendering method as the operating system, the interface and operation mode are closer to the style of the operating system. The cross-platform nature of SWT is addressed through different underlying support libraries.

The Browser class of SWT The
org.eclipse.swt.browser.Browser class is a class used in SWT to implement visual components of web browsers, which can display HTML documents and implement hyperlinks between documents. It mainly has the following methods.
boolean back()
The current session returns to the previous interface in history
boolean execute(String script)
executes a specific script
boolean forward()
The current session advances to the next page in history
void refresh()
Refreshes the current interface
setText(String html )
Display specific HTML content
setUrl(String url)
Display specific web page content, the incoming parameter is the address of the web page

Attach a piece of code for the java swing embedded browser in my own project:

public class BrowserPanel extends JPanel{
    private JPanel webBrowserPanel;  
    private  JWebBrowser webBrowser;  
    private String url;  
    private static BrowserPanel test =  null;
    private BrowserPanel() {  
        super(new BorderLayout());  
        webBrowserPanel = new JPanel(new BorderLayout());  
        webBrowser = new JWebBrowser();  
        webBrowser.setButtonBarVisible(false);  
        webBrowser.setMenuBarVisible(false);  
        webBrowser.setBarsVisible(false);  
        webBrowser.setStatusBarVisible(false);  
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);  
        add(webBrowserPanel, BorderLayout.CENTER);  
        // webBrowser.executeJavascript("javascrpit:window.location.href='http://www.baidu.com'");  
         //webBrowser.executeJavascript("alert('haha')"); //执行Js代码  
    }  
    public static BrowserPanel getInstance(){
        if(test == null){
            test = new BrowserPanel();
        }
        return test;
    }
    public  void openUrl(String url ){
        webBrowser.navigate(url);
    }
    public JPanel getWebBrowserPanel() {
        return webBrowserPanel;
    }
    public void setWebBrowserPanel(JPanel webBrowserPanel) {
        this.webBrowserPanel = webBrowserPanel;
    }
    public JWebBrowser getWebBrowser() {
        return webBrowser;
    }
    public void setWebBrowser(JWebBrowser webBrowser) {
        this.webBrowser = webBrowser;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
}

Summary:
This article introduces four methods for displaying HTML or specific web pages in a Java GUI, including the JEditorPane component in Swing, the implementation of Lobo browser, JDICplus and SWT's Browser component.

For Java developers who are proficient in using SWT, using the browser component in SWT is a good choice. If it is for programmers using Swing, JEditorPane can do the job if it only displays less complicated HTML; if you don't need to consider the portability of the software, you only need to run under Windows, then use JDICplus to browse The browser component is a good choice; if portability is a concern, consider using the Lobo browser.

Reprinted: http://www.ibm.com/developerworks/cn/java/j-lo-browser/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325707981&siteId=291194637