My latest Flickr photograph

A better layout manager in BetterFlowLayout

Thursday 14th of May 2009, 06:00:34 am

Layout management in Java gives me lots of headaches. Especially since I'm writing a custom text annotation tool and there do not seem to be any layout managers that are designed to do what you expect for text-like objects: format as lines and paragraphs.

So I ended up writing my own layout manager, called BetterFlowLayout, which does *almost* the same thing as the normal FlowLayout layout manager but with a crucial difference: you can actually force newlines and line skips. WOW POMS, SO REVOLUTIONARY! I know, there's a few million people on the internet who post on forums asking how to make FlowLayout do linebreaks only to hear people derisively say "that's not what FlowLayout is for".

It damn well should have been, because adding the logic in to make it all work only takes a few hours of programming.

Enjoy. It comes with a main method in it, so you can import it, look at the code (compiled into the jar) and run it to see what it does. It does simple.

JavaDoc is here

What does it look like? Well that's hard to say, since layout managers are responsible for placing components, rather than be visible somehow, but let's imagine we have a large text, chopped up into randomly sized/coloured letters with white backgrounds, and we place them on a dark blue panel. Then we have the BetterFlowLayout manager do its magic with us only saying we want a line spacing of 5px, a paragraph spacing of 15px, and horizontally left justified lines, with the letters vertically bottom aligned (or 'baseline' aligned in the case of text) per line too.

Then we get this:
the result of applying the previous idea