Combining Swing and Apache Pivot
The question of how to combine Swing and Apache Pivot components comes up often enough that I thought I would write a quick blog entry about it.
Pivot doesn't currently support embedded Swing components. However, it is possible to use Pivot components within an existing Swing application. The following demo shows a number of Pivot components hosted in a JInternalFrame; another internal frame hosts their Swing counterparts:
The source code for the example is here:
The SwingDemo class creates an instance of JDesktopPane to which the internal frames are added. The contents of the Pivot frame are defined in pivot_window.bxml and loaded by the createPivotFrame() method. The Swing components are constructed programmatically in createSwingFrame(). Both a main() method and an applet (SwingDemo$HostApplet) are provided to demonstrate desktop as well as web deployment. Note that SwingDemo extends the org.apache.pivot.wtk.ApplicationContext class. This is primarily so the application can access and invoke the protected createTimer() method (called in a static initializer), which is required by Pivot animations such as the spinning progress indicator shown in the demo.
The ability to embed Pivot components within a Swing application does not address all use cases. Specifically, it does not support the re-use of existing custom Swing components in new Pivot-based development. However, it may reprensent a better alternative in other situations: it helps provide a clear migration path from Swing to Pivot, allowing features to be added or replaced without requiring a top-down redesign. The Swing container can be maintained indefinitely or eliminated once all Swing-specific functionality has been ported.