---
title: Building the UI
order: 2
layout: page
---
[[application.architecture]]
= Building the UI
Vaadin Framework user interfaces are built hierarchically from components, so that the
leaf components are contained within layout components and other component
containers. Building the hierarchy starts from the top (or bottom - whichever
way you like to think about it), from the [classname]#UI# class of the
application. You normally set a layout component as the content of the UI and
fill it with other components.
[source, java]
----
public class MyHierarchicalUI extends UI {
@Override
protected void init(VaadinRequest request) {
// The root of the component hierarchy
VerticalLayout content = new VerticalLayout();
content.setSizeFull(); // Use entire window
setContent(content); // Attach to the UI
// Add some component
content.addComponent(new Label("Hello! - How are you?",
ContentMode.HTML));
Grid grid = new Grid<>();
grid.setCaption("My Grid");
grid.setItems(GridExample.generateContent());
grid.setSizeFull();
content.addComponent(grid);
content.setExpandRatio(grid, 1); // Expand to fill
}
}
----
The component hierarchy is illustrated in <>.
[[figure.application.architecture.schematic]]
.Schematic diagram of the UI
image::img/ui-schematic.png[width=80%, scaledwidth=100%]
The actual UI is shown in <>.
[[figure.application.architecture.example]]
.Simple hierarchical UI
image::img/ui-architecture-hierarchical.png[width=70%, scaledwidth=90%]
Instead of building the layout in Java, you can also use a declarative design, as described later in <>.
The examples given for the declarative layouts give exactly the same UI layout as built from the components above.
The easiest way to create declarative designs is to use Vaadin Designer.
The built-in components are described in
<<../components/components-overview.asciidoc#components.overview,"User
Interface Components">> and the layout components in
<<../layout/layout-overview.asciidoc#layout.overview,"Managing
Layout">>.
The example application described above just is, it does not do anything. User
interaction is handled with event listeners, as described a bit later in
<>.
[[application.architecture.architecture]]
== Application Architecture
Once your application grows beyond a dozen or so lines, which is usually quite
soon, you need to start considering the application architecture more closely.
You are free to use any object-oriented techniques available in Java to organize
your code in methods, classes, packages, and libraries. An architecture defines
how these modules communicate together and what sort of dependencies they have
between them. It also defines the scope of the application. The scope of this
book, however, only gives a possibility to mention some of the most common
architectural patterns in Vaadin applications.
The subsequent sections describe some basic application patterns. For more
information about common architectures, see
<<../advanced/advanced-architecture#advanced.architecture,"Advanced
Application Architectures">>, which discusses layered architectures, the
Model-View-Presenter (MVP) pattern, and so forth.
[[application.architecture.composition]]
== Compositing Components
User interfaces typically contain many user interface components in a layout
hierarchy. Vaadin provides many layout components for laying contained
components vertically, horizontally, in a grid, and in many other ways. You can
extend layout components to create composite components.
[source, java]
----
class MyView extends VerticalLayout {
TextField entry = new TextField("Enter this");
Label display = new Label("See this");
Button click = new Button("Click This");
public MyView() {
addComponent(entry);
addComponent(display);
addComponent(click);
setSizeFull();
addStyleName("myview");
}
}
// Create an instance of MyView
Layout myview = new MyView();
----
While extending layouts is an easy way to make component composition, it is a
good practice to encapsulate implementation details, such as the exact layout
component used. Otherwise, the users of such a composite could begin to rely on
such implementation details, which would make changes harder. For this purpose,
Vaadin has the special wrappers [classname]#Composite# and
[classname]#CustomComponent#, which hide the content representation.
[source, java]
----
class MyView extends CustomComponent {
TextField entry = new TextField("Enter this");
Label display = new Label("See this");
Button click = new Button("Click This");
public MyView() {
Layout layout = new VerticalLayout();
layout.addComponent(entry);
layout.addComponent(display);
layout.addComponent(click);
setCompositionRoot(layout);
setSizeFull();
}
}
// Create an instance of MyView
MyView myview = new MyView();
----
For a more detailed description of [classname]#Composite# and [classname]#CustomComponent#, see
<<../components/components-customcomponent#components.customcomponent,"Composition
with Composite and CustomComponent">>.
[[application.architecture.navigation]]
== View Navigation
While the simplest applications have just one __view__ (or __screen__), most of them often require several.
Even in a single view, you often want to have sub-views,
for example to display different content.
<> illustrates a typical navigation
between different top-level views of an application, and a main view with
sub-views.
[[figure.application.architecture.navigation]]
.Navigation Between Views
image::img/view-navigation-hi.png[width=80%, scaledwidth=100%]
The [classname]#Navigator# described in <<../advanced/advanced-navigator#advanced.navigator,"Navigating in an Application">> is a view manager that provides a flexible way to navigate between views and sub-views, while managing the URI fragment in the page URL to allow bookmarking, linking, and going back in the browser history.
Often Vaadin application views are part of something bigger.
In such cases, you may need to integrate the Vaadin applications with the other website.
You can use the embedding techniques described in <<../advanced/advanced-embedding#advanced.embedding,"Embedding UIs in Web Pages">>.
[[application.architecture.accessing]]
== Accessing UI, Page, Session, and Service
You can get the UI and the page to which a component is attached to with
[methodname]#getUI()# and [methodname]#getPage()#.
However, the values are [literal]#++null++# until the component is attached to
the UI, and typically, when you need it in constructors, it is not. It is
therefore preferable to access the current UI, page, session, and service
objects from anywhere in the application using the static
[methodname]#getCurrent()# methods in the respective [classname]#UI#,
[classname]#Page#, [classname]#VaadinSession#, and [classname]#VaadinService#
classes.
[source, java]
----
// Set the default locale of the UI
UI.getCurrent().setLocale(new Locale("en"));
// Set the page title (window or tab caption)
Page.getCurrent().setTitle("My Page");
// Set a session attribute
VaadinSession.getCurrent().setAttribute("myattrib", "hello");
// Access the HTTP service parameters
File baseDir = VaadinService.getCurrent().getBaseDirectory();
----
You can get the page and the session also from a [classname]#UI# with
[methodname]#getPage()# and [methodname]#getSession()# and the service from
[classname]#VaadinSession# with [methodname]#getService()#.
The static methods use the built-in ThreadLocal support in the classes.
ifdef::web[]
The pattern is described in <<../advanced/advanced-global#advanced.global.threadlocal,"ThreadLocal Pattern">>.
endif::web[]
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|