Skip to main content

Combining Swing and Apache Pivot

Posted by gkbrown on October 12, 2010 at 5:22 AM PDT

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.

Related Topics >>


Thanks, Greg, very

Thanks, Greg, very useful.
Can you share any further guidance on event management crossing the Pivot/Swing boundary? Use case: legacy, Swing-based, large codebase/API, solid community momentum - desire for a single developer to demonstrate a migration path towards Pivot. To achieve this, there needs to be a clear understanding of how to control legacy classes via a Pivot UI.
In this example you have here, a callback from the Pivot button that pops up a "Hello Swing, I'm Pivot" dialog in the Swing window would be most helpful.

Answering my own question: ..

Answering my own question:
.. I pulled essential bits out of org.apache.pivot.wtk.DesktopApplicationContext.main(), put these in Greg's createPivotFrame(), and followed the org.apache.pivot.tutorials.stocktracker model for guidance. I was able to drive events in both my Pivot & Swing JInternalFrames from my Pivot UI. Here's a link to the code, in case others are interested:

Hi Greg !! thanks for

Hi Greg !! thanks for your great work, I've been struggling with pivot this weekend and I love it, but it's hard for me describing GUI in xml, as I worked always with GUI builders, I need to experiment with all pivot controls and container layouts and learn how they work !!! Nice and great Toolkit !!! I will continue with pivot when I get home :-) !!