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 aorg.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
- Reference the Widget like all the other ones.
Update
- (Thanks to Jeremy Dowdall)
Code-Example
The following snippet show the usage in a very simple JFace-Dialog.
JAVA:
- /**
- * Very Simple Dialog with {@link org.eclipse.swt.widgets.Label}
- * and a Combo that has the capability to show also icons.
- * @author Tom Seidel
- *
- */
- public class SimpleDialog extends {
- private list;
- /**
- * @param parentShell
- * @param list
- */
- protected SimpleDialog (Shell parentShell, list ) {
- super (parentShell );
- this. list = list;
- }
- /* (non-Javadoc)
- * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
- */
- protected createDialogArea ( parent ) {
- comp = ( ) super. createDialogArea (parent );
- comp. setLayout ( new ( 2, true ) );
- textLabel = new (comp,SWT. NONE );
- 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 );
- iter = this. list. iterator ( );
- 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;
- }
- }
http://www.richclient2.de/2006_03_03/enhancing-the-combo-widget-with-images/