Java Tutorial/SWT/Combo

Материал из Java эксперт
Перейти к: навигация, поиск

Adding Items

To add an item to the specified position in the list, use the following:



public void add(String text, int index)





Add method appends an item to the end of the list

The convenient add method appends an item to the end of the list:



public void add(String text)





Create a dropdown Combo

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CreateDropCombo {
  // Strings to use as list items
  private static final String[] ITEMS = { "A", "B", "C", "D" };
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    // Create a dropdown Combo
    Combo combo = new Combo(shell, SWT.DROP_DOWN);
    combo.setItems(ITEMS);
    
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Create a read-only (non-editable) Combo

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ComboReadOnlyCreate {
  // Strings to use as list items
  private static final String[] ITEMS = { "A", "B", "C", "D" };
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    // Create a read-only Combo
    Combo readOnly = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    readOnly.setItems(ITEMS);
    
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Create a "simple" Combo

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ComboSimpleCreate {
  // Strings to use as list items
  private static final String[] ITEMS = { "A", "B", "C", "D" };
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    // Create a "simple" Combo
    Combo simple = new Combo(shell, SWT.SIMPLE);
    simple.setItems(ITEMS);

    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Creating a Combo with Sorted List

import java.util.Arrays;
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.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ComboSortList {
  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", "E", "F" };
    Arrays.sort(ITEMS);
    final Combo combo = new Combo(shell, SWT.DROP_DOWN);
   combo.setItems(ITEMS);
    combo.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        System.out.println("Selected index: " + combo.getSelectionIndex() + ", selected item: "
            + combo.getItem(combo.getSelectionIndex()) + ", text content in the text field: "
            + combo.getText());
      }
      public void widgetDefaultSelected(SelectionEvent e) {
        System.out.println("Default selected index: "
            + combo.getSelectionIndex()
            + ", selected item: "
            + (combo.getSelectionIndex() == -1 ? "<null>" : combo
                .getItem(combo.getSelectionIndex())) + ", text content in the text field: "
            + combo.getText());
        String text = combo.getText();
        if (combo.indexOf(text) < 0) { // Not in the list yet.
          combo.add(text);
          // Re-sort
          String[] items = combo.getItems();
          Arrays.sort(items);
          combo.setItems(items);
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}



The org.eclipse.swt.custom.CCombo class can also be used to create combo boxes.CCombos are usually used inside table cells.


Deselect an item

public void deselect(int index)
    public void deselectAll()





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)





Getting Items

The getItem method returns the item at the specified index



public String getItem(int index)





Getting selected item index from Combo

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ComboSelectedIndex {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Show Message Box");
    shell.setLayout(new GridLayout(2, false));
    new Label(shell, SWT.NONE).setText("Icon:");
    final Combo icons = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    icons.add("A");
    icons.add("B");
    icons.add("C");
    icons.select(0);
    new Label(shell, SWT.NONE).setText("Buttons:");
    final Combo buttons = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    buttons.add("1");
    buttons.add("2");
    buttons.select(0);
    new Label(shell, SWT.NONE).setText("Return:");
    final Label returnVal = new Label(shell, SWT.NONE);
    returnVal.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Show Message");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        returnVal.setText("");
        returnVal.setText(icons.getSelectionIndex() + " " + buttons.getSelectionIndex());
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Introducing Combo

Combo boxes, also known as dropdowns, combine the strengths of Text and List.

Combo Styles

StyleDescriptionSWT.DROP_DOWNCreates a Combo whose list "drops down."SWT.READ_ONLYDisallows typing input. Only SWT.DROP_DOWN Combos can be read-only.SWT.SIMPLEDisplay Input Box and List together.


Programmatically select an item from the list

public void select(int index)





Removes the first found item with given text

Removes the first found item with given text:



public void remove(String text)





Removing Items

To remove a single item from the list, use the following:



public void remove(int index)





Retrieve the content text of the text field of a combo

public String getText()





Setting Items

public void setItem(int index, String text)
    public void setItems(String[] items)





To remove multiple items

public void remove(int startIndex, int endIndex)