About Gui4j – a framework for creating GUI with XML in Java

Share

Gui4j is a framework for describing Java Swing GUIs completely in XML. It was developped by beck et all projects.
The graphical user interface is created dynamically based on the XML definitions. Each GUI window is associated with exactly one top-level XML file. Each XML file is associated with a java object that acts as both a Controller and a Model for the GUI.
GUI definitions in the XML files can access all Java methods of the Controller, but Java methods cannot access any GUI elements directly. Events are used to trigger changes in the GUI.

A brief quickstart
First download the gui4j.jar from http://sourceforge.net/projects/gui4j/files/ and include it in your project.
For initialization, a properties file with all possible components is necessary. The file
http://www.gui4j.org/gui4jdocs/gui4jComponents.properties contains all currently possible components.
Include it in your project.

Initialization:
The following lines of code initialize gui4j with an unlimited number of worker threads, with validation of xml files and without detailed logging of method calls:

1
2
3
4
5
6
7
8
9
10
public static Gui4j createGui4j() {
		int numberOfWorkerThreads = -1;
		final boolean validateXML = true;
		final boolean logInvoke = false;
		URL url = ClassLoader.class.getFileURL("gui4jComponents.properties");
 
		logger.info("Creating Gui4j instance.");
		return Gui4jFactory.createGui4j(validateXML, logInvoke,
				numberOfWorkerThreads, url);
	}

Create the view:
After initialization we are going to create a demo view,associated with a xml resource, and a controller.
This is how you can create a gui4j view:

1
2
3
4
	private Gui4jView createGui4jView() {
		final Gui4jView view = getGui4j().createView(RESOUCE_NAME, controller, getTitle(), false);
		return view;
	}

A controller must implement the interface Gui4jController

Construct the XML file
All GUIs must be associated with an XML file. The XML file must contain a component definition with the special guiId TOP attribute. This component is the root of the window (the parent which holds all the components). Then, in a nested tree structure, other components can be added to the GUI. These components are simple XML elements that have attributes and children. A comprehensive list with gui4j elements can be found here .
Usually, these components are layed out inside a layout manager, that behave as java layout managers. For example: borderLayout, cardLayout, gridLayout, flowLayout, gridBagLayout, which are also described in the above link as XML elements.
This is a hello world example XML resource

1xml

Call the controller
Most attributes are based on method calls. In XML this is simply a string which is parsed by the framework and translated into method calls using the reflection API of Java. The framework acts like a simple Java parser.
For example, the label
text=”getHelloWorldText” here calls the getHelloWorld function of the associated
controller

1
2
3
4
 public String getHelloWorldText()
    {
        return "Hello World!";
    }

Or,there is also defined a menuItem element that will show the text ‘Exit’ and
when the user presses the menu item, the method ‘actionExit’ inside the associated controller will be executed:

1
2
3
4
public void actionExit()
{
  ... some action
}

Respond to events

The XMLdefinitions refer to methods and instances in the Java code. The Java code itself has no reference to the XML file and (intentionally) no reference to graphical user interface components. The question arises how the Java code can control the user interface. The answer is by using the event concept.
The method call can be preceeded by one or more events. Events are specified before the method call in braces and separated by comma.
Whenever one of these events is fired (by the Java code), the method re-evaluates its value and applies the change to the graphical component.
For example changing a label’s status:
-suppose we have this label
2xml

and 2 buttons:

3xml

The label’s text will be reevaluated each time the java code fires the eModified event. The eModified event is triggered when the user presses one of the buttons.
For the first button,a file locking operation takes place:

1
2
3
4
public void lockFile(){
	file.lockFile();
	eModified.fireEvent();
}

And for unlocking the file, the following code is executed when pressing the second button:

1
2
3
4
public void unlockFile(){
	file.unlockFile();
	eModified.fireEvent();
}

Both functions fire eModified event. When this event is fired, the function getLockedStatus is reevaluated. The function looks like this:

1
2
3
4
5
6
public String getLockedStatus() {
		if (file.isLocked()) {
			return “locked”;
		} else
			return “unlocked”;
	}

So, according to this function’s response, the initial label changes text to one of the return values.
An event is an instance of type org.gui4j.Event.

You can also learn about exception handling or logging with gui4j by visiting gui4j
official site, and you can find many more tips and tricks here.

Take a look http://www.gui4j.org

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

1 thought on “About Gui4j – a framework for creating GUI with XML in Java”
  • Swatee Shrivastava says:

    plz give the proper example of xml file & how we integrate java code and xml gui file???????

    February 17, 2011 at 8:56 am

Comments are closed.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close