Java Tutorial/SWT/List

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

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

   <source lang="java">

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);
 }

}</source>



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


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

   <source lang="java">

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();
 }

}</source>





Create a multiple-selection list

   <source lang="java">

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();
 }

}</source>





Create a single-selection list and add items

   <source lang="java">

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();
 }

}</source>





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

   <source lang="java">

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();
 }

}</source>





Make List Scroll to the bottom

   <source lang="java">

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();
 }

}</source>





Multiple-Copy Collections

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



   <source lang="java">

public static List nCopies(int n, Object element)</source>



[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

   <source lang="java">

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());
 }

}</source>



[A]
1


Reversing Lists: public static void reverse(List list)

   <source lang="java">

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);
 }

}</source>



[C, B, A]


Select a range

   <source lang="java">

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();
 }

}</source>





Setting Items: Add the items all at once

   <source lang="java">

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();
 }

}</source>





Shuffling Lists: randomly reorder the elements of a list

   <source lang="java">

public static void shuffle(List list) public static void shuffle(List list, Random rnd)</source>



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


Sorting Lists: reordering the elements of a List

   <source lang="java">

public static void sort(List list) public static void sort(List list, Comparator comp)</source>



[A, B, C]