Mega Code Archive

 
Categories / Java Tutorial / SWT
 

Finding Items

Search a combo's list from the given index until an item is found. If no such item is found, -1 is returned. public int indexOf(String text, int startIndex) The indexOf method starts the search from 0: public int indexOf(String text) which is equivalent to indexOf(text, 0); import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ComboFindingItem {   public static void main(String[] args) {     Display display = new Display();     Shell shell = new Shell(display);     shell.setLayout(new FillLayout());     String[] ITEMS = { "A", "B", "C", "D" };     final Combo combo = new Combo(shell, SWT.DROP_DOWN);     combo.setItems(ITEMS);     System.out.println(combo.indexOf("B"));          combo.addSelectionListener(new SelectionListener() {       public void widgetSelected(SelectionEvent e) {         System.out.println(combo.getText());       }       public void widgetDefaultSelected(SelectionEvent e) {         System.out.println(combo.getText());       }     });     shell.open();     while (!shell.isDisposed()) {       if (!display.readAndDispatch()) {         display.sleep();       }     }     display.dispose();   } }