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.TableItem
s 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).
Usage
- Download the JavaFile and copy it into your Java-Project.
- Reference the Widget like all the other ones.
Update
- ImageCombo.java with Corrections for GTK (Thanks to Jeremy Dowdall)
Code-Example
The following snippet show the usage in a very simple JFace-Dialog.
- /**
- * Very Simple Dialog with {@link org.eclipse.swt.widgets.Label}
- * and a Combo that has the capability to show also icons.
- * @author Tom Seidel
- *
- */
- private List list;
- /**
- * @param parentShell
- * @param list
- */
- super(parentShell);
- this.list = list;
- }
- /* (non-Javadoc)
- * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
- */
- GridData gd = new GridData(SWT.BEGINNING, SWT.CENTER, false,false);
- gd.widthHint = 80;
- textLabel.setLayoutData(gd);
- textLabel.setText("Your choice:"); //$NON-NLS-1$
- ImageCombo combo = new ImageCombo(comp, SWT.READ_ONLY | SWT.BORDER);
- while (iter.hasNext()) {
- AbstractBaseElement element = (AbstractBaseElement) iter.next();
- // add text and image to the combo.
- combo.add(element.getId(),ImagecontributionPlugin.getDefault()
- .getImage(ImageContributor.getImageIdByObject(element)));
- }
- combo.setLayoutData(gd);
- return comp;
- }
- }