Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
682 views
in Technique[技术] by (71.8m points)

java - How to locate a WebView in a JFXPanel in a JTabbedPane?

I am working about specific web page, and I want to relate together javafx.embed.swing.JFXPanel with its features and swing GUI. I want to locate jfxPanel to JTabbedPane? Here are two methods for 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);
        }
    });
}

and method where I create JTabbedPane for the browser:

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

Thank you for any help or contribution!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Starting from this example using javafx.scene.web.WebView, I made the following changes to produce the result shown below. The createTab() method ensures that the FXPanel has a reasonable preferred size and that the content is created on the JavaFX thread.

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

In general, other web engine implementations are not supported.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...