Events

Using gui4j, the complete graphical user interface is defined in xml. The xml definitions refer to methods and instances in the Java code. The Java code itself has no reference to the xml file and no reference to graphical user interface components. The question arises how the Java code can control the user interface. The short answer is: It’s not possible. However, the same effect can be achieved using the event concept.

Let us consider the enabled state of a button. Using the Swing API directly, we have a reference to the corresponding button and changing the state means to call the setEnabled(boolean state) method on that button. In gui4j we have (intentionally) no reference to the Swing button, so we can’t call the setEnabled method. However, the xml button definition has an attribute for the enabled state:

<button text=”‘Save'” enabled=”isSaveButtonEnabled” actionCommand=”actionSave”/>

The method isSaveButtonEnabled is executing when the user interface is created. So initially, the button has the correct enabled state. The question still is how to change the state during runtime. All we need is to inform the button that it should re-evaluate the method isSavedButtonEnabled. For this purpose, there is 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:

<button text=”‘Save'” enabled=”{getEvent1,getEvent2}isSaveButtonEnabled” actionCommand=”actionSave”/>

A event is an arbitrary method call (or field access) returning an instance of type org.gui4j.Event. The succeeding methods registers itself by the defined events. 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. Note that several components might use the same event (a field variable eRefresh in the controller instance, e.g.).

In this documentation all attributes with method calls capable of events are denoted by event aware.