且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何轻松地从SWT组合下拉列表中获取数据引用

更新时间:2023-01-13 15:04:44

将数据与组合框项目相关联的一种简单方法是使用JFace模型视图包装器 ComboViewer .

One easy way to associate data with combo box items is to use the JFace model-view wrapper ComboViewer.

然后,按以下顺序设置三件事:

Then, set three things, in this order:

  1. 内容提供者,它指定如何从元素中获取元素您接下来提供的输入.(通常,对于模型,这只是一个 ArrayContentProvider 是数组或集合的 .)
  2. 用作输入的模型.模型中的每个元素都可以引用任意用户数据.
  3. 为元素提供标签的标签提供程序.

例如:

    myCombo.setContentProvider(new ArrayContentProvider());
    myCombo.setInput( myModel );
    myCombo.setLabelProvider(new LabelProvider() {
      @Override
      public String getText(Object element) { ... }
    });

完成此操作后,可以使用一些机制来获取选定的元素-或者可以获取包装好的 Combo ,要求其提供选定的索引,然后使用该索引来访问元素您的模型.

Once you've done that, there are mechanisms to get the selected element -- or you can get the wrapped Combo, ask it for its selected index, and use that index to access an element of your model.