Getting a Movie-Playing JComponent

The previous tasks have used the AWT, which seemingly nobody uses anymore. Many developers will want to create a Swing GUI, and thus they need a movie-playing JComponent. QuickTime for Java can provide one.

How do I do that?

Example 2-3 presents a rewrite of the previous BasicQTPlayer that does everything with Swing equivalents (JFrame instead of Frame, JComponent instead of Component, etc.).

Example 2-3. All-Swing simple movie player

package com.oreilly.qtjnotebook.ch02;
 
import quicktime.*;
import quicktime.app.view.*;
import quicktime.std.movies.*;
import quicktime.io.*;
 
import com.oreilly.qtjnotebook.ch01.QTSessionCheck;
 
import java.awt.*;
import javax.swing.*;
 
public class BasicSwingQTPlayer extends JFrame {
 
  public BasicSwingQTPlayer (Movie m) throws QTException {
      super ("Basic Swing QT Player");
      MoviePlayer mp = new MoviePlayer (m);
      QTJComponent qc = QTFactory.makeQTJComponent (mp);
      JComponent jc = qc.asJComponent( );
      getContentPane( ).add (jc);
      pack( );
  }
 
  public static void main (String[  ] args) {
      try {
          QTSessionCheck.check( );
          QTFile file =
              QTFile.standardGetFilePreview (
          QTFile.kStandardQTFileTypes);
          OpenMovieFile omFile = OpenMovieFile.asRead (file);
          Movie m = Movie.fromFile (omFile);
          JFrame f = new BasicSwingQTPlayer (m);
          f.pack( );
          f.setVisible(true);
          m.start( );
      } catch (Exception e) {
          e.printStackTrace( );
      }
  }
}

Note

Compile and run this example with ant run-ch02-basicswingqtplayer.

This produces a simple movie-player window—as seen in ...

Get QuickTime for Java: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.