Wednesday 30 November 2016

SWT: Combo widget

Combo widget is used to show collection of items (Drop Down list), where user can select an item. The user may also type a value into the combo widget.

A Combo takes less space than a List widget and shows similar information.

You can apply following styles to Combo widget.
Style
Description
DROP_DOWN
Style constant for drop down menu/list behavior
READ_ONLY
Style constant for read-only behavior
SIMPLE
Style constant for simple behavior


Only one of the styles DROP_DOWN and SIMPLE may be specified.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
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 Test {

  private static int xPosition = 30;
  private static int yPosition = 30;
  private static int width = 500;
  private static int height = 30;

  private static int shellWidth = 600;
  private static int shellHeight = 600;

  private static String[] countries = { "India", "Germany", "Canada", "Japan", "Sri Lanka", "Singpore", "Russia" };

  private static void addWidgetsToShell(Shell shell) {

    Label label = new Label(shell, SWT.NONE);
    label.setText("Select your Country : ");
    label.setLocation(xPosition, yPosition);
    label.setSize(150, height);

    Combo combo = new Combo(shell, SWT.DROP_DOWN);
    combo.setItems(countries);
    combo.setBounds(xPosition + 150, 20, 150, 100);
    combo.setText("Select your country");

    yPosition += 100;

    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Submit");
    button1.setLocation(xPosition + 100, yPosition);
    button1.setSize(100, 20);

    button1.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        System.out.println("Country Selected is : " + countries[combo.getSelectionIndex()]);
      }
    });

  }

  public static void main(String[] args) {

    /* Instantiate Display object, it represents SWT session */
    Display display = new Display();

    /*
     * Define Shell, it represent a window, You can add more than one shell
     * to Display
     */
    Shell shell = new Shell(display);
    shell.setSize(shellWidth, shellHeight);

    addWidgetsToShell(shell);

    /* Open shell window */
    shell.open();

    /*
     * Run the event dispatching loop until an exit condition occurs, which
     * is typically when the main shell window is closed by the user.
     */

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

    /* Dispose the display */
    display.dispose();
  }
}



Select any country and submit, you can able to see the selected country in the console.



Previous                                                 Next                                                 Home

No comments:

Post a Comment