Java by API/org.eclipse.swt.widgets/List

Материал из Java эксперт
Версия от 14:16, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

List: addSelectionListener(SelectionListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final List list = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
    list.setBounds(40, 20, 220, 100);
    for (int loopIndex = 0; loopIndex < 9; loopIndex++) {
      list.add("Item Number " + loopIndex);
    }
    final Text text = new Text(shell, SWT.BORDER);
    text.setBounds(60, 130, 160, 25);
    list.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent event) {
        int[] selectedItems = list.getSelectionIndices();
        String outString = "";
        for (int loopIndex = 0; loopIndex < selectedItems.length; loopIndex++)
          outString += selectedItems[loopIndex] + " ";
        text.setText("Selected Items: " + outString);
      }
      public void widgetDefaultSelected(SelectionEvent event) {
        int[] selectedItems = list.getSelectionIndices();
        String outString = "";
        for (int loopIndex = 0; loopIndex < selectedItems.length; loopIndex++)
          outString += selectedItems[loopIndex] + " ";
        System.out.println("Selected Items: " + outString);
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





List: add(String value)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(250, 200);
    s.setText("A Tree Shell Example");
    s.setLayout(new FillLayout(SWT.HORIZONTAL));
    final Tree t = new Tree(s, SWT.SINGLE | SWT.BORDER);
    final TreeItem child1 = new TreeItem(t, SWT.NONE, 0);
    child1.setText("1");
//    child1.setImage(new Image(d, "c:\\icons\\folder.gif"));
    final TreeItem child2 = new TreeItem(t, SWT.NONE, 1);
    child2.setText("2");
    final TreeItem child2a = new TreeItem(child2, SWT.NONE, 0);
    child2a.setText("2A");
    final TreeItem child2b = new TreeItem(child2, SWT.NONE, 1);
    child2b.setText("2B");
    final TreeItem child3 = new TreeItem(t, SWT.NONE, 2);
    child3.setText("3");
    final List l = new List(s, SWT.BORDER | SWT.SINGLE);
    t.addTreeListener(new TreeListener() {
      public void treeExpanded(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
  //      ti.setImage(new Image(d, "c:\\icons\\open.gif"));
      }
      public void treeCollapsed(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
//        ti.setImage(new Image(d, "c:\\icons\\folder.gif"));
      }
    });
    t.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        TreeItem ti = (TreeItem) e.item;
        populateList(ti.getText());
      }
      private void populateList(String type) {
        if (type.equals("1")) {
          l.removeAll();
          l.add("1");
          l.add("2");
        }
        if (type.equals("2")) {
          l.removeAll();
        }
        if (type.equals("2A")) {
          l.removeAll();
          l.add("3");
          l.add("4");
        }
        if (type.equals("2B")) {
          l.removeAll();
          l.add("5");
          l.add("6");
        }
        if (type.equals("3")) {
          l.removeAll();
          l.add("7");
          l.add("8");
        }
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





List: getSelection()

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.List;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 250);
    s.setText("A List Example");
    final List l = new List(s, SWT.MULTI | SWT.BORDER);
    l.setBounds(50, 50, 75, 75);
    l.add("Item One");
    l.add("Item Two");
    l.add("Item Three");
    l.add("Item Four");
    l.add("Item Five");
    final Button b1 = new Button(s, SWT.PUSH | SWT.BORDER);
    b1.setBounds(150, 150, 50, 25);
    b1.setText("Click Me");
    b1.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        String selected[] = l.getSelection();
        for (int i = 0; i < selected.length; i++) {
          System.out.println(selected[i]);
        }
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





List: getVerticalBar()

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    // Create a List with a vertical scroll bar
    List list = new List(shell, SWT.V_SCROLL);
    // Add a bunch of items to it
    for (int i = 0; i < 500; i++) {
      list.add("A list item");
    }
    // Scroll to the bottom
    list.select(list.getItemCount() - 1);
    list.showSelection();
    // Get the scroll bar
    ScrollBar sb = list.getVerticalBar();
    // Add one more item that shows the selection value
    list.add("Selection: " + sb.getSelection());
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





List: removeAll()

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(250, 200);
    s.setText("A Tree Shell Example");
    s.setLayout(new FillLayout(SWT.HORIZONTAL));
    final Tree t = new Tree(s, SWT.SINGLE | SWT.BORDER);
    final TreeItem child1 = new TreeItem(t, SWT.NONE, 0);
    child1.setText("1");
//    child1.setImage(new Image(d, "c:\\icons\\folder.gif"));
    final TreeItem child2 = new TreeItem(t, SWT.NONE, 1);
    child2.setText("2");
    final TreeItem child2a = new TreeItem(child2, SWT.NONE, 0);
    child2a.setText("2A");
    final TreeItem child2b = new TreeItem(child2, SWT.NONE, 1);
    child2b.setText("2B");
    final TreeItem child3 = new TreeItem(t, SWT.NONE, 2);
    child3.setText("3");
    final List l = new List(s, SWT.BORDER | SWT.SINGLE);
    t.addTreeListener(new TreeListener() {
      public void treeExpanded(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
  //      ti.setImage(new Image(d, "c:\\icons\\open.gif"));
      }
      public void treeCollapsed(TreeEvent e) {
        TreeItem ti = (TreeItem) e.item;
//        ti.setImage(new Image(d, "c:\\icons\\folder.gif"));
      }
    });
    t.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        TreeItem ti = (TreeItem) e.item;
        populateList(ti.getText());
      }
      private void populateList(String type) {
        if (type.equals("1")) {
          l.removeAll();
          l.add("1");
          l.add("2");
        }
        if (type.equals("2")) {
          l.removeAll();
        }
        if (type.equals("2A")) {
          l.removeAll();
          l.add("3");
          l.add("4");
        }
        if (type.equals("2B")) {
          l.removeAll();
          l.add("5");
          l.add("6");
        }
        if (type.equals("3")) {
          l.removeAll();
          l.add("7");
          l.add("8");
        }
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





List: select(int index)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    // Create a List with a vertical scroll bar
    List list = new List(shell, SWT.V_SCROLL);
    // Add a bunch of items to it
    for (int i = 0; i < 500; i++) {
      list.add("A list item");
    }
    // Scroll to the bottom
    list.select(list.getItemCount() - 1);
    list.showSelection();
    // Get the scroll bar
    ScrollBar sb = list.getVerticalBar();
    // Add one more item that shows the selection value
    list.add("Selection: " + sb.getSelection());
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





List: setItems(String[] values)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 250);
    s.setText("A Combo Example");
    s.setText("A List Example");
    String items[] = { "Item One", "Item Two", "Item Three", "Item Four", "Item Five" };
    final List l = new List(s, SWT.SINGLE | SWT.BORDER);
    l.setBounds(50, 50, 75, 75);
    l.setItems(items);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





List: showSelection()

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    // Create a List with a vertical scroll bar
    List list = new List(shell, SWT.V_SCROLL);
    // Add a bunch of items to it
    for (int i = 0; i < 500; i++) {
      list.add("A list item");
    }
    // Scroll to the bottom
    list.select(list.getItemCount() - 1);
    list.showSelection();
    // Get the scroll bar
    ScrollBar sb = list.getVerticalBar();
    // Add one more item that shows the selection value
    list.add("Selection: " + sb.getSelection());
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





new List(Shell s, SWT.MULTI | SWT.BORDER)

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.List;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 250);
    s.setText("A List Example");
    final List l = new List(s, SWT.MULTI | SWT.BORDER);
    l.setBounds(50, 50, 75, 75);
    l.add("Item One");
    l.add("Item Two");
    l.add("Item Three");
    l.add("Item Four");
    l.add("Item Five");
    final Button b1 = new Button(s, SWT.PUSH | SWT.BORDER);
    b1.setBounds(150, 150, 50, 25);
    b1.setText("Click Me");
    b1.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        String selected[] = l.getSelection();
        for (int i = 0; i < selected.length; i++) {
          System.out.println(selected[i]);
        }
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new List(Shell s, SWT.SINGLE | SWT.BORDER)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 250);
    s.setText("A List Example");
    String items[] = { "Item One", "Item Two", "Item Three", "Item Four", "Item Five" };
    final List l = new List(s, SWT.SINGLE | SWT.BORDER);
    l.setBounds(50, 50, 75, 75);
    l.setItems(items);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}