At Devoxx 09, Xander Tamminga and I told the tale of the perils in using Swing in an OSGi-based application. The details can be found at http://lsd.luminis.nl/swing-and-osgi .
9. Why’s that?
Different design philosophies & assumptions
OSGi is dynamic by definition
Swing is built to be static
www.devoxx.com
10. How to fix that?
Startlevels
Which may have more impact than desired
May not even solve the problem
All UI in a single bundle
But we don’t want that!
www.devoxx.com
17. Notes
The problem gets worse with complexity
Dependency management helps
You give up flexibility at runtime
Remember to use the EDT correctly
www.devoxx.com
18. That’s it!
See the Luminis LSD blog for details and code
http://lsd.luminis.nl/swing-and-osgi
www.devoxx.com
Editor's Notes
You have a window with a table in it. The table is created before the window sets its look and feel; you need to restart the table-providing bundle before the table’s look and feel is correct.
Even worse: two menu’s with different look and feel’s in a single window.
A table gets put into a scrollpane _before_ this scrollpane is part of the AWT hierarchy; hence, a NPE.
(The ColumnHeader only gets created when the scrollpane receives a ‘notify’ from the window)
A table gets put into a scrollpane _before_ this scrollpane is part of the AWT hierarchy; hence, a NPE.
(The ColumnHeader only gets created when the scrollpane receives a ‘notify’ from the window)
How do we create an OSGi-aware Swing application anyway?
- Startlevels are about all bundles; besides, the NPE before cannot be solved by this.
We start out with an application with consisting of a host and nested children.
We use OSGi to resolve our services.
Host -> Childeren -> and their Childeren
finally, all components can initialize themselves.
Since every component now uses the same service interface, we add some service properties to keep them separate; we could also use subclasses of ComponentProvider
The ComponentProvider is a very simple interfaces, which separates these three steps.
- Some dependency management mechanism makes this easier, since we rely on service resolution (the Apache Felix Dependency Manager has been used successfully in the past)
- Application composition is determined at application start.
- So, don’t create your components on a framework thread