RiverLayout - a simple and flexible Java Layout Manager

Java is great but but designing user interfaces can be a headache, especially forms. Layout managers like FlowLayout and BorderLayout are easy to understand, but rather limited, and how often do you want components to wrap to the next row if a window is narrowed? (as is the case with FlowLayout). GridBagLayout is flexible all right, but apart from being overly complex and requiring one to write essays, one cannot easily modify the layout, say add a component in the middle, without having to adjust cell coordinates for other components.

I wanted a flexible layout manager that anyone can understand intuitively. Why not mimic how text is positioned in a text editor? In an editor words flow from left to right. They are normally separated with spaces, but if you want them aligned in columns you can use tab stops. You can insert line breaks where needed and even paragraph breaks to separate sections of text.

This is how RiverLayout works. Without further talking, look at the example below. It displays a simple form with a centered header and ok button. Two of the fields expand automatically to fill the available space and finally, the labels and fields are nicely aligned in two columns.

Here is the code for the form above. The "constraint strings" that are optionally added as components are added are pretty self explanatory ("br" and "p" are inspired by html). For details, please see the RiverLayout Javadoc.

import java.awt.*;
import javax.swing.*;
import se.datadosen.component.RiverLayout;


JFrame f = new JFrame("Our window");
Container c = f.getContentPane();
c.setLayout(new RiverLayout());

c.add("center", new JLabel("Registration form"));
c.add("p left", new JLabel("Name"));
c.add("tab hfill", new JTextField());
c.add("br", new JLabel("Age"));
c.add("tab", new JTextField(3));
c.add("br vtop", new JLabel("Comment"));
c.add("tab hfill vfill", new JScrollPane(new JTextArea()));
c.add("p center", new JButton("Ok"));
As you can see the code is compact and one can get a feeling for the final result while reading it.

Licensing and Download

RiverLayout is released under the LGPL licence. You may therefore freely use it in both non-commerical and commercial applications and you don't need to open up your source code. Improvements made to RiverLayout should however be returned to the project for the benefit of everyone.

Download RiverLayout - binary and source code (12KB)

Release history

1.1 (2005-05-25) -Bugfix: JScrollPanes were oversized (sized to their containing component) if the container containing the JScrollPane was resized.
1.0 (2005-04-24) Initial release

Applications using RiverLayout

So far RiverLayout is being used extensively in the popular application JAlbum in combination with BoxLayout and BorderLayout. Here are some screenshots. I hope to extend this section as more and more Java developers start using RiverLayout. Enjoy!

Contact etc

RiverLayout is written by me, David Ekholm, Datadosen
If you like it or have some suggestions, please let me know.
You can reach me by mail or phone (although email is preferred):

David Ekholm
Mantalsvägen 33
s-175 50 Järfälla
Phone: +46 8 580 15668. Mobile: +46 70 486 77 38