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

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.

主站蜘蛛池模板: 迁安市| 弥勒县| 卓资县| 阿克陶县| 雅江县| 化州市| 乐陵市| 康保县| 普兰县| 金乡县| 平泉县| 临潭县| 太仓市| 博湖县| 招远市| 满洲里市| 罗甸县| 淮南市| 定日县| 泸水县| 镇江市| 米易县| 同心县| 扬州市| 石狮市| 岳阳县| 马关县| 津南区| 全椒县| 绍兴县| 枣庄市| 石狮市| 卢湾区| 乌拉特后旗| 白河县| 平山县| 万源市| 淮阳县| 上杭县| 屏东市| 华宁县|