Reputation: 31
I am trying to create a media streaming application.
I have a Jframe that contains two JFXPanel's. The JFXPanel to the left contains a WebView that loads a directory of links of sample video clips from www.mediacollege.com. The JFXPanel to the right also contains a WebView that plays video content. The panel to the right is currently just playing an embedded link from 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? 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();
}
});
}
}
Upvotes: 3
Views: 1615
Reputation: 849
You'll need to add a ChangeListener to the WebEngine of the WebView. "JavaFX WebView addHyperlinkListener" explains how to do this.
Upvotes: 5