- Vaadin 7 Cookbook
- Jaroslav Holaň Ond?ej Kvasnovsk?
- 510字
- 2021-07-23 14:19:33
Controlling components over the CSS layout
In some cases, we need to control the CSS style of components programmatically. For example, when we want to create a cloud of the most searched terms or tags in our application, we need to change the size of each tag according to the number of searches. We'll use the CSS layout in that case. Our tag cloud will look like the following screenshot:

How to do it...
Carry out the following steps to create a cloud of tags using the CssLayout
class:
- Create an application with the main UI class called, for example,
Demo
.public class Demo extends UI {…}
- We need our own label with the
fontSize
variable. We create aTagLabel
class that extendsLabel
.public class TagLabel extends Label {…}
- Next we add the
fontSize
attribute and the appropriateget
method.private int fontSize; public int getFontSize() { return fontSize; }
- In the constructor we call the parent's constructor by
super(text)
and pass the value offontSize
. If we want to wrap labels on the line, we have to set thesize
toUndefined
, because the size ofLabel
is naturally set to100
percent and it won't allow wrapping label on the line.public TagLabel(String text, int fontSize) { super(text); this.fontSize = fontSize; setSizeUndefined(); }
- Now we create our
TagCloud
class that extendsCssLayout
.public class TagCloud extends CssLayout {…}
- Main functionality is in the
getCss()
method. We override and modify it according to our needs. We control only instances of theTagLabel
class. Here we create a different CSS style for eachTagLabel
. We setfont-size
andline-height
according to thefontSize
variable. We also add styledisplay: inline-block
which we need for wrapping the component on the line.@Override protected String getCss(Component c) { String css = null; if (c instanceof TagLabel) { TagLabel tag = (TagLabel)c; css = "font-size: " + tag.getFontSize() + "px;"; css += "line-height: " + tag.getFontSize() + "px;"; css += "display: inline-block;"; css += "margin: 3px;"; } return css; };
- Now we can use
TagCloud
in our application in the main UI class. For creatingTagLabel
, we need two arrays. The first is for the names and the second is for the font size. We set thewidth
to150
pixels in this layout.@Override public void init(WrappedRequest request) { String names[] = {"HTML", "Java","Vaadin", "GWT", "CSS", "Javascript"}; int fontSizes[] = {12,20,32,24,17,19}; TagCloud tagCloud = new TagCloud(); for (int i=0; i<names.length; i++){ tagCloud.addComponent(new TagLabel(names[i],fontSizes[i])); } tagCloud.setWidth(150, Unit.PIXELS); setContent(tagCloud); }
That's all. We can run the server and open the application in the web browser.
How it works...
Each component inside the layout is controlled by the getCss()
method. Here we set the style for the TagLabel
objects. Only font-size
and line-height
are changed. The values of these properties are stored in the fontSize
array that is created in the main UI class.
See also
- The API of the
CssLayout
class is available at https://vaadin.com/api/7.0.0/com/vaadin/ui/CssLayout.html - Detailed information about
CssLayout
is on the Vaadin web page at https://vaadin.com/book/vaadin7/-/page/layout.csslayout.html
- 解析QUIC/HTTP3:未來互聯網的基石
- Hands-On Chatbot Development with Alexa Skills and Amazon Lex
- OpenLayers Cookbook
- 物聯網關鍵技術及應用
- Spring Cloud微服務架構進階
- 計算機網絡原理與應用技術
- The Kubernetes Workshop
- Android UI Design
- Learning Windows 8 Game Development
- 網絡AI+:2030后的未來網絡
- 物聯網工程導論(第3版)
- SEO攻略:搜索引擎優化策略與實戰案例詳解
- 從物聯到萬聯:Node.js與樹莓派萬維物聯網構建實戰
- 通信系統實戰筆記:無處不在的信號處理
- 物聯網商業設計與案例