1.2. Example Application Walkthrough

Let us follow the long tradition of first saying "Hello World!" when learning a new programming environment. After that, we can go through a more detailed example that implements the model-view-controller architecture. The two examples given are really simple, but this is mostly because IT Mill Toolkit is designed to make things simple.

Example 1.1. HelloWorld.java

import com.itmill.toolkit.ui.*;

public class HelloWorld extends com.itmill.toolkit.Application {

    public void init() { 
        Window main = new Window("Hello window"); 
        setMainWindow(main);
        main.addComponent(new Label("Hello World!")); 
    }
}

The first thing to note is that the example application extends com.itmill.toolkit.Application class. The Application class is used as the base class for all user applications. Instances of the Application are essentially user sessions, and one is created for each user using the application. In the context of our HelloWorld application, it is sufficient to know that the application is started when the user first accesses it and at that time init method is invoked.

Initialization of the application first creates a new window object and sets "Hello window" as its caption. The window is then set as the main window of the application; an application can actually have many windows. This means that when a user launches the application, the contents of the "main window" are shown to the user in the web page. The caption is shown as the title of the (browser) window.

A new user interface component of class com.itmill.toolkit.ui.Label is created. The label is set to draw the text "Hello World!". Finally, the label is added to the main window. And here we are, when the application is started, it draws the text "Hello World!" to the browser window.

The following screenshot shows what the "Hello World!" program will look like in a web browser.

Before going into details, we should note that this example source code is complete and does not need any additional declaratively defined template files to be run. To run the program, you can just add it to your web application, as explained in Section 4.8, “Setting Up the Application Environment”.