codeoncoffee / Shandor-Xul

Java UI Toolkit with support for Swing, SWT, GWT

Event Handlers (aka. Controllers)

Event handlers are the Java classes that your UI interacts with. Any events defined in the UI will be routed to the apporpriately named Event Handler instance. Below is a basic example to illustrate this interaction.

Sample Xul Document

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window 
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script src="com.package.MyHandler.java" id="myHandler"/>
  
  <button label="click me" onclick="myHandler.doClickMe()"/>
  
  <label id="resultLbl"/>
    
</window>

Sample Event Handler

1
2
3
4
5
6
7
8
public class MyHandler extends AbstractXulEventHandler {

  public void doClickMe(){
    XulTextbox textbox = (XulTextbox) document.getElementById("resultLbl");
    textbox.setLabel("User Clicked the button");
  }

}

Adding Event Handlers Programatically

In the above example you saw how to create a handler in the Xul document. This is convenient for small applications or testing setups. However, for more complex applications that need to control the creation of classes (Spring, IoC), there's another way to register Event Handlers.

Locate the code where you're loading your Xul application. Specifically you need the XulDomContainer instance that was returned from the XulLoader. Create your XulEventHandler classes and pass them to the XulDomContainer.

1
2
MainController controller = new MainController();
container.addEventHandler(controller, "myHandler");

This revision is from 2009-06-22 18:53