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

Time for action – getting in focus

To allow the time zone of the clock widgets to be changed, a drop-down box (known as Combo) as well as a Button will be added to the view. The Combo will be created from a set of ZoneId instances.

  1. Create a timeZones field in the ClockView class:
    private Combo timeZones;
  2. At the end of the createPartControl method, add this snippet to create the drop-down list:
    public void createPartControl(Composite parent) {
      ...
      timeZones = new Combo(parent, SWT.DROP_DOWN);
      timeZones.setVisibleItemCount(5);
      for (String zone : ZoneId.getAvailableZoneIds()) {
        timeZones.add(zone);
      }
    }
  3. Run the target Eclipse and open the Clock View again; a list of time zone names will be shown in a drop-down:
  4. It's conventional to set the focus on a particular widget when a view is opened. Implement the appropriate call in the ClockView method setFocus:
    public void setFocus() {
      timeZones.setFocus();
    }
  5. Run Eclipse and show the Clock View; the time zone drop-down widget will be focused automatically.

What just happened?

Every SWT Control has a setFocus method, which is used to switch focus for the application to that particular widget. When the view is focused (which happens both when it's opened and also when the user switches to it after being in a different view), its setFocus method is called.

Note

As will be discussed in Chapter 7, Creating Eclipse 4 Applications, in E4 the method may be called anything and annotated with the @Focus annotation. Conventionally, and to save sanity, it helps to call this method setFocus.

主站蜘蛛池模板: 大邑县| 肥东县| 十堰市| 河津市| 定安县| 定襄县| 聊城市| 阿图什市| 贵溪市| 米泉市| 元阳县| 宝清县| 株洲县| 岐山县| 衢州市| 甘谷县| 乌拉特中旗| 阜新| 贵定县| 新营市| 丰县| 泗水县| 汕头市| 蓝田县| 南部县| 南澳县| 平阳县| 大宁县| 西丰县| 兴隆县| 崇礼县| 和田市| 柞水县| 文山县| 绥棱县| 平和县| 阿克| 上思县| 桂东县| 凤城市| 夏河县|