Event Handling

This example illustrates the key concept of XWT as markup declarative UI about the separation between UI appearance and event handling.

Create a XWT project first (for more information, please see HelloWorld), then new a button with text 'Click Me!'. The appearance is defined in XWT like below.

<Shell xmlns="http://www.eclipse.org/xwt/presentation"
    xmlns:x="http://www.eclipse.org/xwt"
    x:Class="ui.EventHandler">
    <Shell.layout>
       <GridLayout/>
    </Shell.layout>
    <Button text="Click Me!">
    </Button>
</Shell>

Save the file, and click to view it. It running as below.

Next, we add an event handler to the button.

<Button text="Hello, World!" SelectionEvent="clickButton">

Focus on the event handler code and click , the corresponding java method will generate automatically. We can add event in the method directly.

import org.eclipse.swt.Event;
import org.eclipse.swt.Button;

public class EventHandler {
	public void clickButton(Event event) {
		Button button = (Button)event.widget;
		button.setText("Hello World!");
	}
}

Save the file and click . The view is rendered as following.

Click the button to invoke the method clickButton, the content of the button changes to 'Hello, World!'.