ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button
Java Swing, 2nd Edition

Inside RelativeLayout

by 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.

What's Inside?

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 RelativeLayout):

Figure 1: RelativeLayout Source

The File build.xml

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 ex2 and ex3 run 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.jar so you can easily use RelativeLayout with other projects.
  • ant clean: cleans up any generated files and removes the doc and classes directories.

The Files example2.xml and example3.xml

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.

The Directory lib

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. jdom.jar and xerces.jar) into the lib directory, and RelativeLayout will compile and run properly.

The File test.xml

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 Directory src

The rest of the source is organized under the src directory, so let's move in there and see what we find.

Related Reading

Java Swing
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole

  • The files src/Example1.java, src/Example2.java, and src/Example3.java

    These are the three example programs discussed in Part 1.

  • The File src/Test.java

    This is the test program that works with test.xml as 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.

  • The File src/overview.html

    This package overview document is used by JavaDoc to provide introductory information on the starting page.

  • The Directory src/com

    The Java source for RelativeLayout itself 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 RelativeLayout are 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.

    The relativelayout directory 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 XmlConstraintBuilder to parse constraint specifications expressed as XML.

Pages: 1, 2, 3, 4

Next Pagearrow