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

问题描述:

我一直在寻找一种在现有的Swing-Application中集成Web-Browser-Component的方法,并找到了 WebView 。此外,我在java.net上发现了一篇博文,其中显示如何将Java FX组件集成到Swing应用程序中。所以我想这可能是可行的,但我还没有尝试过。

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. Furthermore I found a blog post on java.net showing how to integrate a Java FX component into a Swing Application . 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?

动机是:我想将一些WebBrowser- 整合到中现有的Swing-Application,其长期目标是完全摆脱整个Java桌面应用程序,将其替换为基于Web的解决方案(计划是将现有方面慢慢转换为网页,然后在WebBrowser中显示 - 除了浏览器骨架之外,除了swing应用程序之外没有任何内容的组件。后端当然仍然是Java: - )

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). The backend of course remains Java :-)

我还没有尝试过,因为我没有时间将JavaFX与我的项目集成(这是一份工作,我们是从长远来看,只是探索替代品fpr,所以我最好在燃烧之前询问。

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 2.0 ,并以某种方式设法在类路径中有 jfxrt.jar

One has to install JavaFX 2.0, and somehow manage to have jfxrt.jar in the Classpath.

以下代码呈现 JFXPanel 。 JFXPanel包含 WebView ,其中加载 google.com

The following code renders a JFXPanel inside a JFrame. The JFXPanel contains a WebView which loads google.com.

但是,至少在我的机器上,WebView感觉相当邋..我正在使用Mac OS X 10.6; JavaFX 2.0仍然处于OS X的测试阶段。

However, at least on my machine, the WebView feels rather sloppy. I'm working on Mac OS X 10.6; JavaFX 2.0 is still in beta for OS X.

替代品我发现包含 MozSwing ,看起来非常有前途,实际上感觉非常快。可悲的是,该项目自2008年以来没有进一步开发,捆绑的XUL跑步者相当陈旧(没有新奇的html 5)。

Alternatives I found include MozSwing, which looked very promising and feels quite fast actually. Sadly the project is not being developed any further since 2008 and the bundled XUL runner is rather old (no new fancy html 5).

这两种方法都是一个噩梦maven,你最好设置你自己的本地存储库。

Both approaches are a nightmare to include via maven, you better setup your own local repository.

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();
            }
        });
    }
}