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
Next, we add an event handler to the button.
<Button text="Hello, World!" SelectionEvent="clickButton">
Focus on the event handler code and click
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
Click the button to invoke the method clickButton, the content of the button changes to 'Hello, World!'.