Monday 28 November 2016

SWT: List widget

List widget is used to display collection of items, where user can select an item from the list. A list can be single (or) multi selected. In a multi selected list, you can select more than one item.

You can apply following styles to a List.
Style
Description
SINGLE
Style constant for single selection behavior in lists and single line support on text fields.
MULTI
Style constant for multi-selection behavior in lists and multiple line support on text fields.

Only one of SINGLE and MULTI may be specified. To add items to a list, you can use the setItems() method, or the add() method.

Ex:
List list1 = new List(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
list1.setItems(countries);

list1.setBounds(xPosition + 150, 20, 150, 100);
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
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);

    List list1 = new List(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    list1.setItems(countries);
    list1.setBounds(xPosition + 150, 20, 150, 100);

    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 Index is : " + countries[list1.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 in the list and click the submit button, you can able to see the country selected by you in the console.



Previous                                                 Next                                                 Home

No comments:

Post a Comment