Let us follow the long tradition of first saying "Hello World!" when learning a new programming environment.
Example 1.1. HelloWorld.java
import com.vaadin.ui.*; public class HelloWorld extends com.vaadin.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 every Vaadin application extends the
com.vaadin.Application
class. Instances of the
Application
are essentially user sessions, and one is
created for each user who uses the application. In the context of our
HelloWorld application, it is sufficient to know that the application object
is created 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" refers to the browser window or tab, although window objects are also used when embedding Vaadin applications to HTML pages. The window is set as the main window of the application; an application can actually have many windows (or tabs or embeddings). This means that when a user opens the application in a browser, 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.
The example creates a new Label
user interface
component, which can show simple text, and sets the text to "Hello
World!". Finally, the label is added to the main window. So, when the
application is opened in a browser, the page shows the text "Hello World!".
The result of the Hello World application is shown in Figure 1.2, “Hello World Application”.
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 package it as a web application and deploy it to a server, as explained in Section 4.8, “Setting Up the Application Environment”.