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

时间:2014-01-12 01:27:00

标签: java javafx

我正在尝试创建媒体流应用程序。

我有一个包含两个JFXPanel的Jframe。左侧的JFXPanel包含一个WebView,用于加载来自www.mediacollege.com的示例视频剪辑链接目录。右侧的JFXPanel还包含播放视频内容的WebView。右边的面板目前正在播放www.mediacollege.com的嵌入式链接。

有人知道如何在点击它们时在左侧JFXPanel中捕获URL,以便我可以将它们交给右侧面板中的WebView进行查看吗? 对此的任何帮助将不胜感激。

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

1 个答案:

答案 0 :(得分:5)

您需要将ChangeListener添加到WebView的WebEngine中。 “JavaFX WebView addHyperlinkListener”解释了如何执行此操作。