Reputation: 65
I'm trying to embed a video in a JDialog box. Eventually I need to embed three seperate videos and therefore I have implemented the functionality in such a way as to define my own JFXPanel implementation for reuse. I am able to run the application and when I debug into it the createScene is being executed but I am not seeing any visible video and I am not getting any errors. I also attempted to output text and that was not visible either. I have been able to get the video I would like to embed to show in a pure JavaFX implementation so I know it is not the encoding or something. Can I please get somebody to review the following code and give me some suggestions. I am using Netbeans as an IDE if that is important. Thanks!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swingjavafxtest;
import javafx.embed.swing.JFXPanel;
/**
*
* @author acarnes
*/
public class SwingJavaFXTestDialog extends javax.swing.JDialog
{
private static final String BURST_OPTION_1_VIDEO = "file:///c:/vids/burst_option1.MP4";
/**
* Creates new form SwingJavaFXTestDialog
*/
public SwingJavaFXTestDialog(java.awt.Frame parent, boolean modal)
{
super(parent, modal);
initComponents();
}
/**
* This method is called from within the constructor to initialize the
* form. WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
{
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(SwingJavaFXTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(SwingJavaFXTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(SwingJavaFXTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(SwingJavaFXTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
SwingJavaFXTestDialog dialog = new SwingJavaFXTestDialog(new javax.swing.JFrame(), true);
JavaFXVideoPanel bo1VideoPanel = new JavaFXVideoPanel(BURST_OPTION_1_VIDEO);
dialog.add(bo1VideoPanel);
dialog.addWindowListener(new java.awt.event.WindowAdapter()
{
@Override
public void windowClosing(java.awt.event.WindowEvent e)
{
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swingjavafxtest;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.media.Track;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
/**
*
* @author acarnes
*/
public class JavaFXVideoPanel extends JFXPanel
{
public JavaFXVideoPanel(String url)
{
super();
final String videoURL = url;
Platform.setImplicitExit(false);
Platform.runLater(new Runnable() {
@Override
public void run() {
createScene(videoURL);
}
});
}
private void createScene(String url)
{
/* Media bo1Media = new Media(url);
MediaPlayer bo1MediaPlayer = new MediaPlayer(bo1Media);
bo1MediaPlayer.setAutoPlay(true);
bo1MediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE);
MediaView bo1MediaView = new MediaView(bo1MediaPlayer);
Group root = new Group();
Scene scene = new Scene(root,400,400);
root.getChildren().add(bo1MediaView);
*/
Group root = new Group();
Scene scene = new Scene(root,Color.ALICEBLUE);
Text text = new Text();
text.setX(40);
text.setY(100);
text.setFont(new Font(25));
text.setText("Welcome JAVAFX!");
root.getChildren().add(text);
this.setScene(scene);
this.setVisible(true);
}
}
Upvotes: 1
Views: 697
Reputation: 159496
Sample Solution
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.*;
import javax.swing.*;
public class SwingMediaPlayer extends JDialog {
private static final String VIDEO =
"http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";
public SwingMediaPlayer(java.awt.Frame parent, boolean modal) {
super(parent, modal);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(() -> {
SwingMediaPlayer dialog = new SwingMediaPlayer(new JFrame(), true);
dialog.add(new JavaFXVideoPanel(VIDEO));
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setSize(400, 300);
dialog.setVisible(true);
});
}
static class JavaFXVideoPanel extends JFXPanel {
public JavaFXVideoPanel(String url) {
super();
Platform.runLater(() -> createScene(url));
}
private void createScene(String url) {
Media media = new Media(url);
MediaView tv = new MediaView(new MediaPlayer(media));
setScene(new Scene(new StackPane(tv)));
tv.getMediaPlayer().play();
}
}
}
Sample Screenshot
Because no example is complete without a big red button which does absolutely nothing . . .
Upvotes: 1