简体   繁体   English

捕获URL以在JavaFX WebView / WebEngine中使用

[英]Capturing a URL for use in JavaFX WebView/WebEngine

I am trying to create a media streaming application. 我正在尝试创建一个媒体流应用程序。

I have a Jframe that contains two JFXPanel's. 我有一个包含两个JFXPanel的Jframe。 The JFXPanel to the left contains a WebView that loads a directory of links of sample video clips from www.mediacollege.com. 左侧的JFXPanel包含一个WebView,用于加载来自www.mediacollege.com的示例视频剪辑链接目录。 The JFXPanel to the right also contains a WebView that plays video content. 右侧的JFXPanel还包含播放视频内容的WebView。 The panel to the right is currently just playing an embedded link from www.mediacollege.com. 右边的面板目前正在播放www.mediacollege.com的嵌入式链接。

Would anybody know how would I capture the URL, s in the left JFXPanel when clicking on them so that I could then hand them over to the WebView in the right Panel for viewing? 有人知道如何在点击它们时在左侧JFXPanel中捕获URL,以便我可以将它们移交给右侧面板中的WebView进行查看吗? Any help on this would be greatly appreciated. 任何有关这方面的帮助将不胜感激。

package exploration_02;

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.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

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

public class Exploration_02 {


    private static final String MEDIA_URL = "http://www.mediacollege.com/video-gallery/testclips/testcardm-snellwilcox.flv";

    private static void initAndShowGUI() {

        //Creating the Frame
        JFrame frame = new JFrame("Exploration_02");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().setLayout(null); 
        //Creating the Panels, buttons not in use.
        final JButton jButton = new JButton("Button A");
        final JButton jButton1 = new JButton("Button B");
        final JFXPanel fxPanel = new JFXPanel();
        final JFXPanel fxPanel1 = new JFXPanel();
        //Adding the panels to the Frame
        frame.add(jButton);
        frame.add(jButton1);
        frame.add(fxPanel);
        frame.add(fxPanel1);

        frame.setVisible(true);
        //Panel and Button Params
        jButton.setSize(new Dimension(200, 27));
        fxPanel.setSize(new Dimension(400, 450));
        fxPanel.setLocation(new Point(0, 27));

        jButton1.setSize(new Dimension(200, 27));
        jButton1.setLocation(new Point(501, 0));
        fxPanel1.setSize(new Dimension(550, 450));
        fxPanel1.setLocation(new Point(501, 27));

        frame.getContentPane().setPreferredSize(new Dimension(1100, 580));
        frame.pack();

        frame.setResizable(false);

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

    }


    /* Creates a WebView and navigates to links site */
    private static void initFX(final JFXPanel fxPanel) {
        Group group = new Group();
        Scene scene = new Scene(group, Color.BLUE);
        fxPanel.setScene(scene);

        WebView webView = new WebView();

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

        // Obtain the webEngine.
        WebEngine webEngine = webView.getEngine();

        webEngine.load("http://www.mediacollege.com/video-gallery/testclips/");




    }
    //Creates a WebView for viewing the media files.
    private static void initFX1(final JFXPanel fxPanel1) {
        Group group1 = new Group();
        Scene scene1 = new Scene(group1, Color.RED);
        fxPanel1.setScene(scene1);

        WebView webView1 = new WebView();

        group1.getChildren().add(webView1);
        webView1.setMinSize(300, 300);
        webView1.setMaxSize(400, 300);

        WebEngine webEngine1 = webView1.getEngine();
        webEngine1.loadContent(
                "<video width='360' height='288'controls='controls'>"
                + "<source src='http://mediacollege.com/video-gallery/testclips/20051210-w50s.flv'/>"
                + "Your browser does not support the video tag."
                + "</video>");


    }

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

You'll need to add a ChangeListener to the WebEngine of the WebView. 您需要将ChangeListener添加到WebView的WebEngine。 " JavaFX WebView addHyperlinkListener " explains how to do this. JavaFX WebView addHyperlinkListener ”解释了如何执行此操作。

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

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