如何在JTabbedPane中的JFXPanel中找到WebView?

时间:2017-05-20 14:24:43

标签: java swing javafx

我正在研究特定的网页,我想将javafx.embed.swing.JFXPanel与其功能和swing GUI联系在一起。我想找jfxPanelJTabbedPane?以下是JFXPanel的两种方法。

private JFXPanel getSearchPanel() {
    jfxPanel = new JFXPanel();
    createScene();
    setLayout(new BorderLayout());
    jfxPanel.setVisible(true);
    return jfxPanel;
}

private void createScene() {
    PlatformImpl.startup(new Runnable() {
        @Override
        public void run() {
            Pane root = new WebViewPane();
            Scene scene = new Scene(root, 80, 50);
            jfxPanel.setScene(scene);
        }
    });
}

以及为浏览器创建JTabbedPane的方法:

private void createNewTab() {
        JPanel panel = new JPanel(new BorderLayout());
        startPage = startPageField.getText();
        panel.add(new JScrollPane(getSearchPanel()), BorderLayout.CENTER);
        tabPane.setBackground(Color.PINK);
        tabPane.addTab(startPage, panel);   
    }

感谢您的帮助或贡献!

1 个答案:

答案 0 :(得分:2)

从使用examplejavafx.scene.web.WebView开始,我进行了以下更改以生成如下所示的结果。 createTab()方法确保FXPanel具有合理的首选大小,并且内容是在JavaFX线程上创建的。

JTabbedPane jtp = new JTabbedPane();
jtp.add("One", createTab("http://www.example.com"));
jtp.add("Two", createTab("http://www.example.net"));
jtp.add("Three", createTab("http://www.example.org"));
frame.add(jtp, BorderLayout.CENTER);

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

/**
 * @see https://stackoverflow.com/a/44091479/230513
 * @see https://stackoverflow.com/a/31576647/230513
 */
public class WebViewTest {

    private void initAndShowGUI() {
        // This method is invoked on the EDT thread
        JFrame frame = new JFrame("WebViewTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTabbedPane jtp = new JTabbedPane();
        jtp.add("One", createTab("http://www.example.com"));
        jtp.add("Two", createTab("http://www.example.net"));
        jtp.add("Three", createTab("http://www.example.org"));
        frame.add(jtp, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JFXPanel createTab(String s) {
        final JFXPanel fxPanel = new JFXPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        Platform.runLater(() -> {
            initFX(fxPanel, s);
        });
        return fxPanel;
    }

    private void initFX(JFXPanel fxPanel, String s) {
        // This method is invoked on the JavaFX thread
        StackPane root = new StackPane();
        Scene scene = new Scene(root);
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.load(s);
        root.getChildren().add(webView);
        fxPanel.setScene(scene);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new WebViewTest()::initAndShowGUI);
    }
}

通常,其他Web引擎实现是not supported