简体   繁体   English

将JavaFX 2.0 WebView集成到Swing Java SE 6应用程序中

[英]Integrating JavaFX 2.0 WebView into a Swing Java SE 6 Application

I was looking for a way to integrate a Web-Browser-Component in an existing Swing-Application and found WebView for Java FX 2.0. 我一直在寻找一种方法在现有的Swing-Application中集成Web-Browser-Component,并找到了WebView for Java FX 2.0。 Furthermore I found a blog post on java.net showing how to integrate a Java FX component into a Swing Application . 此外,我在java.net上发现了一篇博客文章,展示了如何将Java FX组件集成到Swing应用程序中 So I guess it might be doable, but I haven't tried yet. 所以我想这可能是可行的,但我还没有尝试过。

I'm curious, do you think this is a good approach? 我很好奇,你认为这是一个好方法吗? Are there any better solutions? 还有更好的解决方案吗? Is it even doable? 它甚至可行吗? Is maybe something prebundled out there? 也许是预先分类的东西?

The motivation is: I want to integrate some WebBrowser- whatever into an existing Swing-Application, the long-term goal being to get rid of the whole Java Desktop Application at all, replacing it with a web-based solution (the plan is to slowly convert existing aspects into webpages which are then displayed in the WebBrowser-Component until nothing is left of the swing application except for the browser-skeleton). 的动机是:我想整合一些WebBrowser- 无论到现有的Swing应用中,长期目标被达到所有摆脱整个Java桌面应用程序的,与基于Web的解决方案替换它(该计划是慢慢地将现有方面转换为网页,然后将其显示在WebBrowser-Component中,直到除了浏览器骨架之外没有任何剩余的swing应用程序。 The backend of course remains Java :-) 后端当然仍然是Java :-)

I haven't tried yet since I simply lack the time to integrate JavaFX with my project (its a job, we're just exploring alternatives fpr the long run), so I better ask before I get burned. 我还没有尝试过,因为我没有时间将JavaFX与我的项目集成(这是一项工作,我们只是在长期探索替代方案),所以我最好在我被烧毁之前询问。

It is very well possible! 这很有可能!

One has to install JavaFX 2.0 , and somehow manage to have jfxrt.jar in the Classpath. 一个人必须安装JavaFX 2.0 ,并以某种方式设法在Classpath中使用jfxrt.jar

The following code renders a JFXPanel inside a JFrame. 以下代码在JFrame中呈现JFXPanel The JFXPanel contains a WebView which loads google.com . JFXPanel包含一个加载google.comWebView

However, at least on my machine, the WebView feels rather sloppy. 但是,至少在我的机器上,WebView感觉相当邋.. I'm working on Mac OS X 10.6; 我正在使用Mac OS X 10.6; JavaFX 2.0 is still in beta for OS X. JavaFX 2.0仍处于OS X测试阶段。

Alternatives I found include MozSwing , which looked very promising and feels quite fast actually. 我找到的替代品包括MozSwing ,它看起来很有前途,实际上感觉非常快。 Sadly the project is not being developed any further since 2008 and the bundled XUL runner is rather old (no new fancy html 5). 遗憾的是,该项目自2008年以来没有进一步开发,捆绑的XUL跑者相当陈旧(没有新的花哨的HTML 5)。

Both approaches are a nightmare to include via maven, you better setup your own local repository. 这两种方法都是通过maven包含的噩梦,您最好设置自己的本地存储库。

import java.awt.Dimension;
import java.awt.Point;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class JavaFX {

    /* Create a JFrame with a JButton and a JFXPanel containing the WebView. */
    private static void initAndShowGUI() {
        // This method is invoked on Swing thread
        JFrame frame = new JFrame("FX");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().setLayout(null); // do the layout manually

        final JButton jButton = new JButton("Button");
        final JFXPanel fxPanel = new JFXPanel();

        frame.add(jButton);
        frame.add(fxPanel);
        frame.setVisible(true);

        jButton.setSize(new Dimension(200, 27));
        fxPanel.setSize(new Dimension(300, 300));
        fxPanel.setLocation(new Point(0, 27));

        frame.getContentPane().setPreferredSize(new Dimension(300, 327));
        frame.pack();
        frame.setResizable(false);

        Platform.runLater(new Runnable() { // this will run initFX as JavaFX-Thread
            @Override
            public void run() {
                initFX(fxPanel);
            }
        });
    }

    /* Creates a WebView and fires up google.com */
    private static void initFX(final JFXPanel fxPanel) {
        Group group = new Group();
        Scene scene = new Scene(group);
        fxPanel.setScene(scene);

        WebView webView = new WebView();

        group.getChildren().add(webView);
        webView.setMinSize(300, 300);
        webView.setMaxSize(300, 300);

            // Obtain the webEngine to navigate
        WebEngine webEngine = webView.getEngine();
        webEngine.load("http://www.google.com/");
    }

    /* Start application */
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }
}

If you need to embed Swing lightweight web browser based on Chromium engine, you can take a look at JxBrowser library. 如果您需要嵌入基于Chromium引擎的Swing轻量级Web浏览器,您可以查看JxBrowser库。 The following code demonstrates how to embed Browser component into JFrame and load some web page: 以下代码演示了如何将Browser组件嵌入到JFrame中并加载一些Web页面:

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

/**
 * This sample demonstrates how to create Browser instance,
 * embed it into Swing BrowserView container, display it in JFrame and
 * navigate to the "www.google.com" web site.
 */
public class BrowserSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.loadURL("http://www.google.com");
    }
}

The web page will be rendered by Chromium engine. 该网页将由Chromium引擎呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM