Good advice: buy that book
So, just after waking up, in the usual last ten minutes before getting to work, I had the idea to check whether the book could help me in fixing a problem that was around since a few days (actually I was working on it only during breaks, but you get the point). If you read my latest blog post, you know that I'm working on mixing the Visual Library with JXMapViewer from SwingLabs. The two components should be rendered one over the other, and for this kind of stuff there's JLayeredPane. Unfortunately, it didn't worked well for me, I presumed for something related to the LayoutManager. So I developed a customized LayoutManager and went for some customized code extending JPanel (quite a common (anti)pattern for people working with Swing). Yesterday I discovered that the stuff wasn't working properly on Linux (don't know if because of the different operating system or because I tested it with Java 6); more code changes followed; then I added a third layer to render the navigation controls and the stuff got completely broken. Despair: this stuff must be demoed next week, so I scheduled a "massive revert" thinking of giving up with some parts of the demo.
This morning I took the book, went to the "Layered Panes" chapter, "Layered Panes and Layouts" paragraph. Here's the illuminating excerpt:
The only efficient way to use a layout with a layered pane are to write your own or to use the least-known layout of all time,
Yep, it was indeed the least-known of all time, I often navigate inside Java sources in search of inspiration but I always missed it! The result is that these lines work for me:
add(backgroundComponent, new Integer(100));
add(sceneComponent, new Integer(200));
add(foregroundComponent, new Integer(300));
Got rid of the code bloat, now everything is simpler, it works everywhere, it can be demoed next week ;-) Thanks Chet and Romain!
Photo courtesy of Geertjan Wielenga