无痕客

落花无情,流水无痕……

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Probably you was already faced with the requirement to add small icons to your pulldowns. That looks very nice, but unfortunately there is no common SWT-Widget to realize this.
Fortunately the Eclipse-Framework is OpenSource and we can reprodruce the structure of a SWT-ComboBox. A Combo is not more than a text-field and a small button with an arrow. In addition is a event-handler implemented that shows a Composite as a tooltip with the entries of the "combo-list". We just have to take this class and change the structure of the content. We don't want to have a org.eclipse.swt.widgets.List, but a org.eclipse.swt.widgets.Table with multiple org.eclipse.swt.widgets.TableItems where you can specify an image. After adjusting the access-methods we have a new cool Widget, that has the same structure and methods like the "built-in"s. :)

Screenshot

image_combo.png
The ImageCombo in Action (as Widget in a JFace-Dialog).

Usage

Update

Code-Example

The following snippet show the usage in a very simple JFace-Dialog.

JAVA:
  1. /**
  2. * Very Simple Dialog with {@link org.eclipse.swt.widgets.Label}
  3. * and a Combo that has the capability to show also icons.
  4. * @author Tom Seidel
  5. *
  6. */
  7. public class SimpleDialog extends Dialog {
  8.      private List list;
  9.      /**
  10.       * @param parentShell
  11.       * @param list
  12.       */
  13.      protected SimpleDialog(Shell parentShell, List list) {
  14.          super(parentShell);
  15.          this.list = list;
  16.      }
  17.      /* (non-Javadoc)
  18.       * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
  19.       */
  20.      protected Control createDialogArea(Composite parent) {
  21.          Composite comp = (Composite) super.createDialogArea(parent);
  22.          comp.setLayout(new GridLayout(2,true));
  23.          Label textLabel = new Label(comp,SWT.NONE);
  24.          GridData gd = new GridData(SWT.BEGINNING, SWT.CENTER, false,false);
  25.          gd.widthHint = 80;
  26.          textLabel.setLayoutData(gd);
  27.          textLabel.setText("Your choice:"); //$NON-NLS-1$
  28.          ImageCombo combo = new ImageCombo(comp, SWT.READ_ONLY | SWT.BORDER);
  29.          Iterator iter = this.list.iterator();
  30.          while (iter.hasNext()) {
  31.              AbstractBaseElement element = (AbstractBaseElement) iter.next();
  32.              // add text and image to the combo.
  33.              combo.add(element.getId(),ImagecontributionPlugin.getDefault()
  34.                  .getImage(ImageContributor.getImageIdByObject(element)));
  35.          }
  36.          combo.setLayoutData(gd);
  37.         return comp;
  38.     }
  39. }
转载:
http://www.richclient2.de/2006_03_03/enhancing-the-combo-widget-with-images/
posted on 2012-03-07 23:14  无痕客  阅读(406)  评论(0编辑  收藏  举报