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

Time for action – adding some infrastructure

Edit the init method of your TimeItUI class to match the following:

@Override
protected void init(VaadinRequest request) {
  initCombo();
  initButton();
  initLayout();
}

What just happened?

Here we are breaking up the functionality to initialize our UI components in a more suitable way by implementing smaller methods. Now that we have the required infrastructure, we can start adding input components.

Comboboxes

A combobox is an input component that allows users to select an option from a drop-down list. It looks like the following screenshot:

You must be thinking "yeah right, but how do you put those options in there?" This is easy:

ComboBox c = new ComboBox("Select an option");
c.addItem("Option one");
c.addItem("Option two");
c.addItem("Option three");

If you have the options in a Java Collection class, you can pass the Collection instance to the constructor like this:

ArrayList<String> options = new ArrayList<>();
options.add("Option 1");
options.add("Option 2");
options.add("Option 3");
ComboBox c = new ComboBox("Select an option", options);

The method addItem accepts an Object, so we can pass instances of any class. For example, if we had a User class we could have done this:

User user1 = new User("Jurka", "Rahikkala");
User user2 = new User("Joonas", "Lehtinen");

ComboBox c = new ComboBox("Select a user");
c.addItem(user1);
c.addItem(user2);

Now, how will the ComboBox component know what to print as option captions? First, let's go back to our "Time It" application.

主站蜘蛛池模板: 广安市| 德江县| 玉环县| 聂荣县| 朝阳市| 明星| 舞钢市| 永年县| 开江县| 锡林郭勒盟| 乐东| 宁远县| 图木舒克市| 梁河县| 武夷山市| 甘德县| 辉南县| 资溪县| 阿瓦提县| 界首市| 精河县| 庆安县| 阳江市| 美姑县| 泽普县| 隆化县| 额尔古纳市| 呼图壁县| 家居| 石泉县| 凉山| 会理县| 郸城县| 巴东县| 连平县| 宁南县| 阿鲁科尔沁旗| 宝清县| 方城县| 溧水县| 横山县|