422
|
Chapter 11, Native Integration and Packaging
#83 Control iTunes Under Windows
HACK
Working with COM
The Component Object Model (COM) is a standard way for Windows com-
ponents to expose functionality that other programs can call at runtime.
com4j (https://com4j.dev.java.net/) is an open source library that creates
connections from Java programs to COM objects. com4j has two parts: a
command-line program to create the Java interfaces that your program will
call, and a native library that binds your program to the COM object at run-
time.
com4j uses class annotations to do its magic, so you can only
use it with Java 5.0 or greater.
To get started, download the com4j package at https://com4j.dev.java.net/
servlets/ProjectDocumentList. With the com4j stubber and the iTunes exe-
cutable in your current directory, you can generate the interfaces like this:
java -jar tlbimp.jar -o jtunes -p test.jtunes iTunes.exe
This command will load the iTunes executable and look for COM defini-
tions. Once they are located,
tlbimp will generate a bunch of Java interfaces
in the
test.jtunes package and put the .java files into the jtunes directory. If
you look at the generated Java interfaces, you will see a whole slew of meth-
ods and objects for playing, querying tracks, and dealing with virtually every
other feature of iTunes. com4j will also pull out any embedded documenta-
tion and insert the documentation as JavaDoc comments in the generated
interfaces.
This process is pretty quick, so you may find it useful to call
it from Ant as part of your compile process.
Once you have the interface stubs, you can create a program to control
iTunes quite easily. You can use the same program that you did when con-
trolling iTunes on the Mac
[Hack #82]. Just replace the action listener with the
class in Example 11-6.
Example 11-6. A listener to control iTunes on Windows
import test.jtunes.*;
public class WinItunes {
public void actionPerformed(ActionEvent evt) {
try {

Get Swing Hacks 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.