Java Tutorial/SWT/List — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:20, 31 мая 2010

Copying Lists: public static void copy(List dest, List src)

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainClass {
  public static void main(String[] a) {
    List l1 = Arrays.asList(new String[] { "A", "B" ,"C"});
    List l2 = Arrays.asList(new String[] { "A", "C", "D" });
    Collections.copy(l1, l2); // works
    Collections.copy(l2, l1); //
    System.out.println(l1);
    System.out.println(l2);
  }
}



[A, C, D]
[A, C, D]


Create a List with a vertical ScrollBar and Add a bunch of items to it

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.Shell;
public class ListCreateAddItemsToIt {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    // Create a List with a vertical ScrollBar
    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");
    }

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





Create a multiple-selection list

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.Shell;
public class CreateMultipleSelectionList {
  // Strings to use as list items
  private static final String[] ITEMS = { "A", "B", "C", "D" };
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    // Create a multiple-selection list
    List multi = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);

    // Add the items, one by one
    for (int i = 0, n = ITEMS.length; i < n; i++) {
      multi.add(ITEMS[i]);
    }
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Create a single-selection list and add items

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.Shell;
public class CreateSingleSelectionListAddItems {
  // Strings to use as list items
  private static final String[] ITEMS = { "A", "B", "C", "D" };
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    // Create a single-selection list
    List single = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
    // Add the items, one by one
    for (int i = 0, n = ITEMS.length; i < n; i++) {
      single.add(ITEMS[i]);
    }
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Introducing List and List Styles

List boxes display lists of strings and allow users to select one or more of them.

StyleEffectSWT.BORDERDraws a border around this List.SWT.SINGLECreates a List that allows selection of only one item at a time. This is the default.SWT.MULTICreates a List that allows selection of multiple items at a time. You may specify only one of SWT.SINGLE or SWT.MULTI.SWT.H_SCROLLCreates a horizontal scrollbar to scroll this List.SWT.V_SCROLLCreates a vertical scrollbar to scroll this List.


List Item Single Selection

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.Shell;
public class ListItemSelection {
  // Strings to use as list items
  private static final String[] ITEMS = { "A", "B", "C", "D" };
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    // Create a single-selection list
    List single = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
    // Add the items, one by one
    for (int i = 0, n = ITEMS.length; i < n; i++) {
      single.add(ITEMS[i]);
    }
    // Select the third item
    single.select(2);
    
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Make List Scroll to the bottom

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.Shell;
public class ListScrollBottom {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    // Create a List with a vertical ScrollBar
    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();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Multiple-Copy Collections

The nCopies() method of Collections is similar to using fill(), then copy(), with a List:



public static List nCopies(int n, Object element)



[null, null, null, null, null, null, null, null, null, null]


public static void fill(List list, Object element): method copies the same object reference to every element of the list

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainClass {
  public static void main(String[] a) {
    List list = new ArrayList(10);
    Object anObject = "A";
    list.add("N");
    Collections.fill(list, anObject);
    System.out.println(list);
    System.out.println(list.size());
  }
}



[A]
1


Reversing Lists: public static void reverse(List list)

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainClass {
  public static void main(String[] a) {
    List barFlies = Arrays.asList(new String[] { "A", "B", "C" });
    Collections.reverse(barFlies);
    System.out.println(barFlies);
  }
}



[C, B, A]


Select a range

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.Shell;
public class SelectRangeList {
  // Strings to use as list items
  private static final String[] ITEMS = { "A", "B", "C", "D" };
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    // Create a multiple-selection list
    List multi = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);

    // Add the items all at once
    multi.setItems(ITEMS);

    // Select a range
    multi.select(0, 2);
    
    
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Setting Items: Add the items all at once

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.Shell;
public class SettingItemsAddItemAtOnce {
  // Strings to use as list items
  private static final String[] ITEMS = { "A", "B", "C", "D" };
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    // Create a multiple-selection list
    List multi = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);

    // Add the items all at once
    multi.setItems(ITEMS);
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Shuffling Lists: randomly reorder the elements of a list

public static void shuffle(List list)
public static void shuffle(List list, Random rnd)



[H, D, A, E, C, B, I]
[H, D, A, E, C, B, I]


Sorting Lists: reordering the elements of a List

public static void sort(List list)
public static void sort(List list, Comparator comp)



[A, B, C]