« I hate Swing | Main | Palm clock synchronization »

Better layout managers

After my previous ranting I realized that maybe a different layout manager was what I was looking for. Most of the existing layout managers are so stupid that you need to create lots of nested JPanels just to achieve the layout that you want. (First, I need, overall, one thing in the middle to grow, so I'll use a BorderLayout. Within that, I need a top-down layout, so I'll use the BoxLayout. Within that I need one part that's left-to-right, so I'll stick with the default FlowLayout...)

Screw that. Let's find one layout manager that's powerful enough to do what I want without multiple nested panels, but architected sanely enough to actually use. A tall order? For Java, of course.

There were few real choices. If I had to reject GridBagLayout because it just isn't flexible enough to provide pixel-level positioning, then there isn't much left. About the only thing left was the SpringLayout. It's new in 1.4, and it's powerful and rather complicated to use. I found a nice article explaining its use, but I started losing it about two pages in.

The one interesting thing is that the article actually recommended a totally different layout manager--something called the RelativeLayout. It's a free layout with fairly nice provisions. Redistribute and use it all you want, just provide proper attributions. I read through an explanatory article, and found it simple to understand but quite powerful. You add your items to the container, and then just provide a list of relations that must be obeyed for laying out the items.

So I switched to the RelativeLayout, and things are mostly working now. I'm still having trouble getting my maximum size for the window to be obeyed, but at least it's obeying my minimum. At last I can get to actually putting some code into the controller to make the UI work.

Post a comment