Table des matières

JFace

-- Composants

-- ListViewer

Instanciation

    lvTest = new ListViewer(compositeParent, SWT.BORDER | SWT.V_SCROLL);
    List listTest = lvTest.getList();

Affectation du contenu et définition du ContentProvider

	lvCategories.setContentProvider(new ArrayContentProvider());
	lvCategories.setInput(getModeles());

package net.ex.categorie.main;

import java.util.ArrayList;

public class MyApp2 {

	protected Shell shell;
	private Table tableProduits;
	private java.util.List<Categorie> categories;
	private ListViewer lvCategories;
	private Categorie categorieActive;
	private TableColumnLayout tLayout;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MyApp2 window = new MyApp2();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(523, 406);
		shell.setText("SWT Application");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashMain = new SashForm(shell, SWT.NONE);

		Composite compoLeft = new Composite(sashMain, SWT.NONE);
		compoLeft.setLayout(new FillLayout(SWT.HORIZONTAL));

		lvCategories = new ListViewer(compoLeft, SWT.BORDER | SWT.V_SCROLL);
		List listCategories = lvCategories.getList();
		listCategories.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection sel = (StructuredSelection) lvCategories.getSelection();
				categorieActive = (Categorie) sel.getFirstElement();
			}
		});

		Composite compoRight = new Composite(sashMain, SWT.NONE);
		compoRight.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(compoRight, SWT.VERTICAL);

		Composite composite = new Composite(sashForm, SWT.NONE);
		tLayout = new TableColumnLayout();
		composite.setLayout(tLayout);

		TableViewer tvProduits = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
		tableProduits = tvProduits.getTable();
		tableProduits.setHeaderVisible(true);
		tableProduits.setLinesVisible(true);

		createColumn(tableProduits, "Nom", 10);
		createColumn(tableProduits, "Couleur", 5);

		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		sashForm.setWeights(new int[] { 1, 1 });
		sashMain.setWeights(new int[] { 2, 5 });

		loadCategories();
	}

	private void createColumn(Table table, String caption, int weight) {
		TableColumn col = new TableColumn(table, SWT.NONE);
		col.setText(caption);
		tLayout.setColumnData(col, new ColumnWeightData(10));
	}

	public void loadCategories() {
		lvCategories.setContentProvider(new ArrayContentProvider());
		lvCategories.setInput(getCategories());
	}

	public java.util.List<Categorie> getCategories() {
		categories = new ArrayList<>();
		categories.add(new Categorie("catégorie 1", new String[] { "elem1", "elem2" }));
		categories.add(new Categorie("catégorie 2", new String[] { "elem3" }));
		categories.add(new Categorie("catégorie 3", new String[] { "elem4" }));
		return categories;
	}
}