Java Tutorial/SWT/Combo
Содержание
- 1 Adding Items
- 2 Add method appends an item to the end of the list
- 3 Create a dropdown Combo
- 4 Create a read-only (non-editable) Combo
- 5 Create a "simple" Combo
- 6 Creating a Combo with Sorted List
- 7 Deselect an item
- 8 Finding Items
- 9 Getting Items
- 10 Getting selected item index from Combo
- 11 Introducing Combo
- 12 Programmatically select an item from the list
- 13 Removes the first found item with given text
- 14 Removing Items
- 15 Retrieve the content text of the text field of a combo
- 16 Setting Items
- 17 To remove multiple items
Adding Items
To add an item to the specified position in the list, use the following:
<source lang="java">
public void add(String text, int index)</source>
Add method appends an item to the end of the list
The convenient add method appends an item to the end of the list:
<source lang="java">
public void add(String text)</source>
Create a dropdown Combo
<source lang="java">
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(); }
}</source>
Create a read-only (non-editable) Combo
<source lang="java">
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(); }
}</source>
Create a "simple" Combo
<source lang="java">
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(); }
}</source>
Creating a Combo with Sorted List
<source lang="java">
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(); }
}</source>
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
<source lang="java">
public void deselect(int index)
public void deselectAll()</source>
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.
<source lang="java">
public int indexOf(String text, int startIndex)</source>
Getting Items
The getItem method returns the item at the specified index
<source lang="java">
public String getItem(int index)</source>
Getting selected item index from Combo
<source lang="java">
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(); }
}</source>
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
<source lang="java">
public void select(int index)</source>
Removes the first found item with given text
Removes the first found item with given text:
<source lang="java">
public void remove(String text)</source>
Removing Items
To remove a single item from the list, use the following:
<source lang="java">
public void remove(int index)</source>
Retrieve the content text of the text field of a combo
<source lang="java">
public String getText()</source>
Setting Items
<source lang="java">
public void setItem(int index, String text)
public void setItems(String[] items)</source>
To remove multiple items
<source lang="java">
public void remove(int startIndex, int endIndex)</source>