- Vaadin 7 Cookbook
- Jaroslav Holaň Ond?ej Kvasnovsk?
- 357字
- 2021-07-23 14:19:34
Aligning components on a page
Aligning components is easy in Vaadin. We can align them on the left, on the right, on the top, on the bottom, and also center them vertically or horizontally. In this recipe, we will create a demo application in which we can see how aligning works. We will create three buttons in three different positions, as we can see in the following screenshot:

How to do it...
Carry out the following steps to create and learn how alignment works in Vaadin.
- We create a Vaadin project with the main UI class named
Demo
.public class Demo extends UI {…}
- We create a class called
AligningDemo
that is based on theVerticalLayout
.public class AligningDemo extends VerticalLayout {…}
- In the constructor, we create and add all three buttons. The first button is placed on the top left side. We'll do it by the
setComponentAlignment()
method. As a parameter, we use predefined alignments from theAlignment
class.public AligningDemo() { Button leftButton = new Button("top, left"); addComponent(leftButton); setComponentAlignment(leftButton, Alignment.TOP_LEFT);
- The next button is centered in the middle of the page.
Button centerButton = new Button("middle, center"); addComponent(centerButton); setComponentAlignment(centerButton, Alignment.MIDDLE_CENTER);
- And the last button is aligned at the bottom on the right.
Button rightButton = new Button("bottom, right"); addComponent(rightButton); setComponentAlignment(rightButton, Alignment.BOTTOM_RIGHT);
- For a better view, we enable layout margins on all four sides of the layout and we stretch the layout to fill the screen.
setMargin(true); setSizeFull(); }
- That's all. Now we can use our
AligningDemo
class in the root class.public class Demo extends UI { @Override public void init(VaadinRequest request) { setContent(new AligningDemo()); } }
How it works...
We can set the alignment of the component inside a specific layout with the setComponentAlignment()
method. The method takes the component contained in the layout to be formatted as its parameters, and also takes the horizontal and vertical alignment. The easiest way to set alignments is to use the constants defined in the Alignment
class.
See also
- Additional information about aligning in the layout is described on the Vaadin web page at https://vaadin.com/book/vaadin7/-/page/layout.settings.html#layout.settings.alignment
- API of the
Alignment
class, available at https://vaadin.com/api/7.0.0/com/vaadin/ui/Alignment.html
- Hands-On Chatbots and Conversational UI Development
- OpenLayers Cookbook
- 網(wǎng)絡(luò)創(chuàng)新指數(shù)研究
- 面向云平臺(tái)的物聯(lián)網(wǎng)多源異構(gòu)信息融合方法
- 計(jì)算機(jī)網(wǎng)絡(luò)工程實(shí)用教程(第2版)
- 網(wǎng)絡(luò)基礎(chǔ)與網(wǎng)絡(luò)管理項(xiàng)目化教程
- Bonita Open Solution 5.x Essentials
- Building Web Applications with ArcGIS
- jQuery Mobile Web Development Essentials
- Master Apache JMeter:From Load Testing to DevOps
- SAE原理與網(wǎng)絡(luò)規(guī)劃
- Getting Started with tmux
- Enterprise ApplicationDevelopment with Ext JSand Spring
- 路由與交換技術(shù)
- Scala Programming Projects