o7planning

Java SWT Combo Tutorial with Examples

  1. SWT Combo
  2. Combo Example
  3. Handling events with Combo

1. SWT Combo

Combo is a component of popular interface. It allows users to select one of options. When users click on Combo, a list of options will appear for users to select.
Note: All items of SWT Combo are strings, if you want these items are type of Object, you can use JFace ComboViewer which is a class wrapping a Combo.
See more JFace ComboViewer:
  • JFace ComboViewer
Styles:
Style
Description
SWT.DROP_DOWN
Creates a Combo whose list "drops down."
SWT.READ_ONLY
Disallows typing input. Only SWT.DROP_DOWN Combos can be read-only.
SWT.SIMPLE
Display Input Box and List together.
You can create Combo from a list of strings:
// Create a dropdown Combo
Combo combo = new Combo(shell, SWT.DROP_DOWN);

String[] items = new String[] { "English", "Vietnamese" }; 
combo.setItems(items);
You can also add more items to the Combo:
// Adds the item to the begin of the receiver's list.
combo.add("Indian",0);

// Adds the item to the end of the receiver's list.
combo.add("Indian");

2. Combo Example

The following example creates a drop-down Combo, can enter additional values.
ComboDemo.java
package org.o7planning.swt.combo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
 

public class ComboDemo {

    public static void main(String[] args) {

        Display display = new Display();
        Shell shell = new Shell(display);

        RowLayout rowLayout = new RowLayout();
        rowLayout.marginLeft = 10;
        rowLayout.marginTop = 10;
        shell.setLayout(rowLayout);

        Label label = new Label(shell,SWT.NONE );
        
        label.setText("Select language:");
        
        // Create a dropdown Combo
        Combo combo = new Combo(shell, SWT.DROP_DOWN);

        String[] items = new String[] { "English", "Vietnamese" };

        combo.setItems(items);

        shell.setText("SWT Combo (o7planning.org)");
        shell.setSize(400, 200);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    
}
Readonly Combo:
// Create a dropdown Combo & Read only
Combo combo = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);

String[] items = new String[] { "English", "Vietnamese" };
combo.setItems(items)
Simple Combo
// Create a simple Combo
Combo combo = new Combo(shell, SWT.SIMPLE | SWT.READ_ONLY);

String[] items = new String[] { "English", "Vietnamese" };
combo.setItems(items);

3. Handling events with Combo

For examle, when a Combo displays a list of languages, users can enter more new language then press Enter to add it to list.
ComboEventDemo.java
package org.o7planning.swt.combo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class ComboEventDemo { 
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        RowLayout rowLayout = new RowLayout();
        rowLayout.marginLeft = 10;
        rowLayout.marginTop = 10;
        shell.setLayout(rowLayout);
        // Label
        Label label = new Label(shell, SWT.NONE);
        label.setText("Select language:");

        // Create a dropdown Combo
        Combo combo = new Combo(shell, SWT.DROP_DOWN);
        String[] items = new String[] { "English", "Vietnamese" };
        combo.setItems(items); 
        Label labelMessage = new Label(shell, SWT.NONE);

        // User select a item in the Combo.
        combo.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int idx = combo.getSelectionIndex();
                String language = combo.getItem(idx);
                labelMessage.setText("Select: " + idx + " - " + language);
                labelMessage.pack();
            }
        });
        // User enter new 'language'
        // and press Enter to add new 'language' to the list.
        combo.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // User pressed Enter.
                if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
                    String text = combo.getText();
                    if (text == null || text.length() == 0) {
                        return;
                    }
                    String[] items = combo.getItems();
                    for (String item : items) {
                        if (item.equals(text)) {
                            return;
                        }
                    }
                    combo.add(text);
                }
            }
        });
        shell.setText("SWT Combo (o7planning.org)");
        shell.setSize(400, 200);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}