官术网_书友最值得收藏!

  • Vaadin 7 Cookbook
  • Jaroslav Holaň Ond?ej Kvasnovsk?
  • 515字
  • 2021-07-23 14:19:33

Creating a custom layout

When we work on a complex web application, we need to cooperate with more people in the team. UX or graphic designers design layouts and for them it is more natural to design layouts using HTML and CSS. In such cases, we can use Custom layout that is described in the HTML template.

Creating a custom layout

How to do it...

Carry out the following steps to create a custom layout:

  1. Create a project with the main UI class, Demo.
    public class Demo extends UI {…}
  2. First, we'll create an HTML template. Vaadin separates the appearance of the user interface from its logic using themes. Themes can include Sass or CSS style sheets, custom HTML layouts, and any necessary graphics. We'll call our template mylayout.html and place it under the folder layouts. In the WebContent folder we create this path of folders:
    WebContent/VAADIN/themes/mytheme/layouts
  3. Next, we define our layout. By setting the location attribute in the <div> element, we mark our specific areas. These elements will be replaced by Vaadin components. On the top we will put a header. We will create one menu on the left side. We will leave the central area for some content page. At the end we will insert a page footer, for example, for a status line. The Attribute class is used for CSS styling.
       <div location="header" class="header"></div>
       <div location="menu" class="menu"></div>
       <div location="content" class="content"></div>
       <div location="footer" class="footer"></div>
  4. In the next step, we create our CSS style for this layout. Under the folder mytheme we create a file styles.css.
    WebContent/VAADIN/themes/mytheme/styles.css
  5. In this file, we can say how to align a component in each area, what color and size will be used, and other properties.
    .header,.menu,.footer {
      border: thin;
      border-style: solid;
      border-color: LightGrey;
    }
    
    .header {
      text-align: center;
      font-size: 32px;
      height: 75px;
    }
    
    .menu {
      height: 300px;
      width: 20%;
      text-align: center;
      float: left;
    }
    
    .content {
      text-align: left;
    }
    
    .footer {
      text-align: right;
      clear: both;
    }
  6. Now we will create a simple Vaadin project with a main UI class called Demo. We will add the annotation mytheme, which means that we use this theme in our application. In the init() method, we will set CustomLayout as a content. Each component is added by the addComponent() method, where the second parameter is the location in the HTML template.
    @Theme("mytheme")
    public class Demo extends UI {
      @Override
      public void init(VaadinRequest request) {
        CustomLayout layout = new CustomLayout("mylayout");
        setContent(layout);
    
        Label header = new Label("Custom layout");
        header.addStyleName("header");
        layout.addComponent(header, "header");
    
        Label menu = new Label("menu");
        layout.addComponent(menu, "menu");
    
        Label content = new Label("This is content of page.");
        layout.addComponent(content, "content");            
    
        Label footer = new Label("Created by Vaadin, 2013");
        layout.addComponent(footer, "footer");      
      }
    }

How it works...

Layout is described in the HTML template file. A template includes the <div> elements with a location attribute that defines the location identifier. The client-side engine of Vaadin will replace the contents of the location elements with the components. The components are bound to the location elements by the location identifier given to the addComponent() method. The template file is separate from the source code. It's placed under the WebContent/VAADIN/themes/<nameTheme>/layouts folder. We can set the style of the layout with the CSS file placed in WebContent/VAADIN/themes/<nameTheme>/styles.css.

主站蜘蛛池模板: 织金县| 卓尼县| 马边| 布拖县| 宜章县| 泰顺县| 西峡县| 聂拉木县| 马龙县| 华容县| 横峰县| 长岭县| 蒙自县| 新晃| 玛纳斯县| 苍溪县| 修文县| 肃宁县| 胶南市| 望谟县| 凉城县| 青海省| 宁强县| 吴桥县| 三原县| 望江县| 酒泉市| 邯郸县| 信宜市| 报价| 乌拉特中旗| 津南区| 曲松县| 阿克苏市| 克拉玛依市| 萨迦县| 南城县| 长治县| 阿荣旗| 丹江口市| 山阳县|