Sample List : List « SWT JFace Eclipse « Java






Sample List

Sample List

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Feb 8, 2004 8:21:31 PM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.com/swt
 *****************************************************************************/



import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
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 SampleList {
  Display display = new Display();
  Shell shell = new Shell(display);

  public SampleList() {
    init();
    
    RowLayout rowLayout = new RowLayout();
    shell.setLayout(rowLayout);
    
    (new Label(shell, SWT.NULL)).setText("What programming languages are you proficient in? ");
    
    final List list = new List(shell, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
    
    String[] languages = new String[]{"Java", "C", "C++", "SmallTalk"};
    
    for(int i=0; i<languages.length; i++)
      list.add(languages[i]);
    
    list.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        System.err.println(list.getSelectionIndex());
        int[] indices = list.getSelectionIndices();
        String[] items = list.getSelection();
        StringBuffer sb = new StringBuffer("Selected indices: ");
        for(int i=0; i < indices.length; i++) {
          sb.append(indices[i]);
          sb.append("(");
          sb.append(items[i]);
          sb.append(")");
          if(i == indices.length-1)
            sb.append('.');
          else
            sb.append(", ");
        }
        System.out.println(sb.toString());
      }

      public void widgetDefaultSelected(SelectionEvent e) {
        int[] indices = list.getSelectionIndices();
        String[] items = list.getSelection();
        StringBuffer sb = new StringBuffer("Default selected indices: ");
        for(int i=0; i < indices.length; i++) {
          sb.append(indices[i]);
          sb.append("(");
          sb.append(items[i]);
          sb.append(")");
          if(i == indices.length-1)
            sb.append('.');
          else
            sb.append(", ");
        }
        System.out.println(sb.toString());
      }
    });
    
    list.selectAll();
    //list.select(1);

    shell.pack();
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }

  private void init() {

  }

  public static void main(String[] args) {
    new SampleList();
  }
}


           
       








Related examples in the same category

1.List DemoList Demo
2.Single Multi ListsSingle Multi Lists
3.Demonstrates ListsDemonstrates Lists
4.List ExampleList Example
5.List Example 2List Example 2
6.List Example 3List Example 3
7.SWT List Example DemoSWT List Example Demo
8.SWT List Selection EventSWT List Selection Event
9.SWT List Composite
10.Print selected items in a listPrint selected items in a list