SWT List Composite : List « SWT JFace Eclipse « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » SWT JFace Eclipse » List 




SWT List Composite

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

public class Ch8ListComposite extends Composite {
  public Ch8ListComposite(Composite parent) {
    super(parent, SWT.NULL);
    populateControl();
  }

  protected void populateControl() {
    FillLayout compositeLayout = new FillLayout();
    setLayout(compositeLayout);

    int[] styles = SWT.SINGLE, SWT.MULTI };

    for (int style = 0; style < styles.length; style++) {
      createListViewer(styles[style]);
    }
  }

  private void createListViewer(int style) {
    ListViewer viewer = new ListViewer(this, style);

    viewer.setLabelProvider(new LabelProvider() {
      public String getText(Object element) {
        return ((ListItemelement).name;
      }
    });

    viewer.addFilter(new ViewerFilter() {
      public boolean select(Viewer viewer, Object parent, Object element) {
        return ((ListItemelement).value % == 0;
      }
    });

    viewer.setSorter(new ViewerSorter() {
      public int compare(Viewer viewer, Object obj1, Object obj2) {
        return ((ListItemobj2).value - ((ListItemobj1).value;
      }
    });

    viewer.setContentProvider(new IStructuredContentProvider() {

      public Object[] getElements(Object inputElement) {
        return ((ListinputElement).toArray();
      }

      public void dispose() {
      }

      public void inputChanged(Viewer viewer, Object oldInput,
          Object newInput) {
      }
    });

    List input = new ArrayList();
    for (int i = 0; i < 20; i++) {
      input.add(new ListItem("item " + i, i));
    }

    viewer.setInput(input);
  }

}

class ListItem {
  public String name;

  public int value;

  public ListItem(String n, int v) {
    name = n;
    value = v;
  }
}

           
       














Related examples in the same category
1.List DemoList Demo
2.Single Multi ListsSingle Multi Lists
3.Sample ListSample List
4.Demonstrates ListsDemonstrates Lists
5.List ExampleList Example
6.List Example 2List Example 2
7.List Example 3List Example 3
8.SWT List Example DemoSWT List Example Demo
9.SWT List Selection EventSWT List Selection Event
10.Print selected items in a listPrint selected items in a list
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.