In this topic will show you how to get started with XWT by developing a "Hello World" step by step.
First of all, we need to create a Java project to host our application. If there is already one, you can reuse it without any additional configuration. In this sample, we name our Java project "Hello".
Then, right click the Java project, select the context menu New → Other... → XWT → New Mediator and click Next button.
In the pop-up dialog, fill the Package as 'demo' and Name as 'Hello'.
Click the Finish button. We create a Mediator which consists of the two files:
The Hello.xwt is a UI resource file, and Hello.java is the associated Java class to handle relative events.
Now our project looks like below.
Open Hello.xwt file, switch on the “Source” mode edition in the XWT UI editor to modify directly the XML code. This editor provides a powerful code completion and palette tools to help the UI design.
Modify the XWT code as below:
<Composite xmlns="http://www.eclipse.org/xwt/presentation" xmlns:x="http://www.eclipse.org/xwt" xmlns:j="clr-namespace:demo" x:Class="demo.Hello"> <Composite.layout> <FillLayout /> </Composite.layout> <Button text="Hello, world!"> </Button> </Composite>
If you are a SWT developer, you can find the mapping from XML to SWT library is direct 1-1. It gives the result as the corresponding code in Java:
Composite composite = new Composite(parent, SWT.NONE); composite.addLayout(new FillLayout()); Button button = new Button(composite, SWT.NONE); button.setText("Hello, world!");
Save the file, and click
The load and open a Mediator in API is pretty straightforward. We need call the services of the class XWT: load() or open() .
Create a new Java class named ‘Application’ with main method and you can use the following codes to load and open our Mediator.
public class Application { public static void main(String[] argus){ URI content = Hello.class.getResource("Hello.xwt"); Shell shell = XWT.load(content).getShell(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!shell.getDisplay().readAndDispatch()) { shell.getDisplay().sleep(); } } } }
Or simply
public class Application { public static void main(String[] argus){ URI content = Hello.class.getResource("Hello.xwt"); try { XWT.open(content); } catch (Exception e) { e.printStackTrace(); } } }
This is the "HelloWorld" project, please click here for download.