Enhancing the Combo-Widget with Images

March 3rd, 2006

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

The ImageCombo in Action (as Widget in a JFace-Dialog)
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. /**
  3. * Very Simple Dialog with {@link org.eclipse.swt.widgets.Label}
  4. * and a Combo that has the capability to show also icons.
  5. * @author Tom Seidel
  6. *
  7. */
  8. public class SimpleDialog extends Dialog {
  9. private List list;
  10. /**
  11. * @param parentShell
  12. * @param list
  13. */
  14. protected SimpleDialog(Shell parentShell, List list) {
  15. super(parentShell);
  16. this.list = list;
  17. }
  18. /* (non-Javadoc)
  19. * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
  20. */
  21. protected Control createDialogArea(Composite parent) {
  22. Composite comp = (Composite) super.createDialogArea(parent);
  23. comp.setLayout(new GridLayout(2,true));
  24. Label textLabel = new Label(comp,SWT.NONE);
  25. GridData gd = new GridData(SWT.BEGINNING, SWT.CENTER, false,false);
  26. gd.widthHint = 80;
  27. textLabel.setLayoutData(gd);
  28. textLabel.setText("Your choice:"); //$NON-NLS-1$
  29.  
  30. ImageCombo combo = new ImageCombo(comp, SWT.READ_ONLY | SWT.BORDER);
  31.  
  32. Iterator iter = this.list.iterator();
  33. while (iter.hasNext()) {
  34. AbstractBaseElement element = (AbstractBaseElement) iter.next();
  35. // add text and image to the combo.
  36. combo.add(element.getId(),ImagecontributionPlugin.getDefault()
  37. .getImage(ImageContributor.getImageIdByObject(element)));
  38. }
  39. combo.setLayoutData(gd);
  40. return comp;
  41. }
  42. }
  43.  

Entry Filed under: SWT

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

March 2006
M T W T F S S
« Jan   Apr »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Most Recent Posts