Inside RelativeLayoutby James Elliott, coauthor of Java Swing, 2nd Edition
As promised in my first article, "RelativeLayout: A Constraint-Based Layout Manager," here's a look inside the
RelativeLayout package. This article explains how the layout manager works, and discusses how to extend it to support new kinds of constraints. Readers should be familiar with the original article, which introduces
RelativeLayout and explains how to use it as a tool.
Once you download and expand the source archive, you'll find the following items inside of it (Figure 1 shows everything it will contain once you're ready to build and run
Figure 1: RelativeLayout Source
This is a build file for the Ant tool from Apache's Jakarta project. It is used to compile and test
RelativeLayout. Once you have installed Ant on your system (which you have likely done already, since it has rapidly and deservedly become the build tool of choice for Java projects) you can compile
RelativeLayout simply by moving to the top-level source directory and typing
ant compile (after you've set up the
lib directory as described below).
Other interesting build targets you can run include:
ant ex1: runs the first example program discussed in the first article. Similarly, the targets
ex3run the second and third examples.
ant doc: builds the JavaDoc for
RelativeLayout. You may want to refer to this documentation from time to time as you read the overview of how the classes work, below.
ant dist: builds the distribution file
RelativeLayout.jarso you can easily use
RelativeLayoutwith other projects.
ant clean: cleans up any generated files and removes the
These files are used by the XML-based examples in the first article. They contain the layout constraints used by the second and third example programs.
Contains libraries used by
RelativeLayout. It's empty when you first download and expand the source archive, because these libraries are available from separate organizations. In order to compile and use
RelativeLayout, you'll need the JDOM library and (if you're using a Java SDK earlier than version 1.4) an XML parser such as Apache Xerces, as discussed in the first article. Once you've downloaded any libraries you need (which you likely did in order to run the examples when reading Part 1), copy their library jars (e.g.
xerces.jar) into the
lib directory, and
RelativeLayout will compile and run properly.
I used this file along with a test program while I was developing
RelativeLayout. It's not too useful now, unless you want to study and play with that test program. Note that the current configuration of the program (invoked through
ant test) and this file are inconsistent and cause an over-constraint error to be reported. If you're into that sort of thing, debugging and fixing the problem could be an interesting exercise.
The rest of the source is organized under the
src directory, so let's move in there and see what we find.
- The files
These are the three example programs discussed in Part 1.
This is the test program that works with
test.xmlas described above. It's no longer of much interest except for software archaeology, in that it provides a little insight into the development of the package.
This package overview document is used by JavaDoc to provide introductory information on the starting page.
The Java source for
RelativeLayoutitself is grouped under this directory. To be precise, it's in the nested directory src/com/brunchboy/util/swing/relativelayout, corresponding to the package in which the classes themselves are organized,
com.brunchboy.util.swing.relativelayout. The classes that make up
RelativeLayoutare explained in the next few sections. You'll best understand how everything works if you can examine the source itself while you read the descriptions below, perhaps by printing one or the other.
relativelayoutdirectory also contains the file
package.html, used by JavaDoc to provide an introductory explanation for the classes in the directory, and
constraint-set.dtd, the XML document type definition (described below), used by
XmlConstraintBuilderto parse constraint specifications expressed as XML.