CIT 597 Assignment 8: XML GUI Builder
Fall 2005, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Java GUIs are created by code in Java programs. Two traditional ways of making this Java code are:

But there are newer, XML-based ways:

A web search for XML GUI will turn up several XML-based GUI builders. One of the advantages of such a program is that it allows HTML writers who are not Java programmers to create Java GUIs.

Code generation is not a technique that is limited to large, complex tools. Rather, it is an approach you can use whenever you are faced with writing long, routine pieces of code. In this assignment you will write (as a .java text file) a complete class definition, but in general you might generate only a list of statements to be inserted into a program.

Your assignment will be to write your own tiny GUI builder, not as a useful program, but as a "proof of concept." It will handle only three types of components (panels, buttons, and text areas) and three kinds of layout manager (flow, grid, and border).

Outline of assignment:

Your assignment is to write the following DTD and two classes:


Describes what the XML file should look like for the GUI builder.


Uses the XmlGuiBuilder class (after it has been compiled) to construct and display the GUI.

Also write a suitable XML file for testing out your GUI builder. This XML file should describe one GUI and should make use of all of the features of your GUI builder.


Your GUI builder should handle the following:

The DTD and XML files:

Your XML file should specify how components (panels, buttons, and text areas) are arranged in the GUI, and provide the necessary information for each component. You design how the XML file is to be written, and specify this in your DTD. You don't have to exactly mimic the Java GUI code; for example, since every panel has an associated layout manager, you might combine these two things into a single XML tag (such as "flowPanel").

In the XML file, specify the name of the class to be generated. This name will also be used as the name of the generated output file (with a .java extension).

Note: This assignment assumes that you are using DTDs to describe your XML, but any other schema language, such as XSL or RELAX NG, is also acceptable.

The XmlGuiBuilder program:

Your XmlGuiBuilder application might be fairly long, but it shouldn't be too mysterious. It simply reads in the XML file (using SAX, DOM, XOM, or any other technology you like), translates the XML to Java code, and writes out the result as a text file (with a .java extension). The XmlGuiBuilder program will probably contain statements such as:

System.out.println("import javax.swing.*;");
System.out.println("import java.awt.*;");
System.out.println("import java.awt.event.*;");
System.out.println("public class " + className + "{");
panelName = "panel" + panelNumber;
System.out.println("JPanel " + panelName + " = new JPanel();");
System.out.println(panelName + ".add(" + buttonName + ");");

Your generated code should contain an inner class that implements ActionListener and creates one instance of this inner class. Add this listener to every button you create. The actionPerformed method of your inner class should just print out the label on the button that was pressed (see javax.swing.AbstractButton.getText()).

The complexity of your code will depend in part on the complexity of the XML, so design the XML (as specified in the DTD) as simply and cleanly as possible.

It may help to remember that instance variables may be declared anywhere in the class, and local variables may be defined just before they are needed. You don't have to declare all variables at the top of a class or method.

Your components can have generated names (such as "panel" + panelNumber), or you can give them names in the XML file and use those names in the generated code. This is your choice, and you should do whatever is easiest for you.

Since I'm asking for a DTD anyway, it's probably a good idea to use a validating parser. This should help you catch any errors you may make in the XML.

The generated .java file:

The generated Java code will define a class containing a method that creates a specific GUI. Code generated from different XML files will create different GUIs. The generated code will not be a complete application (it won't have a main method). It does have a method that, when called, creates and returns a JPanel representing the complete GUI. (A JPanel is much more flexible than a JFrame, and can be used, for example, in an applet, or as a component of a larger GUI.)

After you have generated the .java file, you need to compile it, by whatever means is most convenient for you. Your XmlGuiBuilder program is not expected to compile it for you.

Remember, this program is intended as a proof of concept: The point is to show your manager (and yourself) whether you can build such a thing. Hence you do not need error checking (beyond what you need to help debug the program) or beautiful output. You do not need to generate nicely formatted, stylistically beautiful code.

The XmlGuiTester program:

This is a trivial program that gets a JPanel from the generated Java class, puts it into a JFrame, and displays it. Closing the JFrame should end the program.

For simplicity, you can hardwire the name of the generated Java class into your test program.

Due date:

After Thanksgiving--Sunday, November 27, before midnight.