Table
Einfaches Beispiel für eine Tabelle.
(Basis ist wieder die Source-Beispiel-Applikation; die dort vorhandene Klasse MyEditorPage muss nur durch die Klasse MyEditorPageTable ersetzt werden.)
package com.sowas.javawiki.rcpbaseapplication; import java.util.ArrayList; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.ui.forms.IManagedForm; import org.eclipse.ui.forms.editor.FormEditor; import org.eclipse.ui.forms.editor.FormPage; import org.eclipse.ui.forms.editor.IFormPage; import org.eclipse.ui.forms.widgets.FormToolkit; public class MyEditorPageTable extends FormPage implements IFormPage { private Table table; private FormToolkit formToolkit; private TableViewer tableViewer; private Addresses addresses; private String[] columnNames = {"Nachname", "Vorname"}; public MyEditorPageTable(FormEditor editor, String pageId, String pageTitle) { super(editor, pageId, pageTitle); addresses = new Addresses(); } @Override protected void createFormContent(IManagedForm managedForm) { super.createFormContent(managedForm); Composite client = managedForm.getForm().getBody(); client.setLayout(new GridLayout(2, false)); formToolkit = managedForm.getToolkit(); Label label = formToolkit.createLabel(client, "Tabelle:"); GridData gdLabel = new GridData(GridData.FILL_VERTICAL); gdLabel.verticalAlignment = SWT.TOP; label.setLayoutData(gdLabel); table = formToolkit.createTable(client, SWT.BORDER); GridData gridData = new GridData(GridData.FILL_BOTH); gridData.heightHint = 100; table.setLayoutData(gridData); table.setHeaderVisible(true); table.setLinesVisible(true); int col = 0; TableColumn tcLastName = new TableColumn(table, SWT.LEFT, col); tcLastName.setText(columnNames[col]); col++; TableColumn tcFirstName = new TableColumn(table, SWT.LEFT, col); tcFirstName.setText(columnNames[col]); for (int i =0; i < table.getColumnCount(); i++) table.getColumn(i).pack(); tableViewer = new TableViewer(table); tableViewer.setColumnProperties(columnNames); tableViewer.setContentProvider(new IStructuredContentProvider() { @Override public Object[] getElements(Object inputElement) { System.out.println("getElements()"); return ((Addresses)inputElement).getAddesses(); } @Override public void dispose() { } @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } }); tableViewer.setLabelProvider(new ITableLabelProvider() { @Override public Image getColumnImage(Object element, int columnIndex) { return null; } @Override public String getColumnText(Object element, int columnIndex) { String text = ""; Address addr = (Address) element; switch (columnIndex) { case 0: text = addr.getLastName(); break; case 1: text = addr.getFirstName(); break; } System.out.println(text); return text; } @Override public void addListener(ILabelProviderListener listener) { } @Override public void dispose() { } @Override public boolean isLabelProperty(Object element, String property) { return false; } @Override public void removeListener(ILabelProviderListener listener) { } }); tableViewer.setInput(addresses); tableViewer.refresh(); } } class Addresses { ArrayList<Address> addresses = new ArrayList<Address>(); public Addresses() { addresses.add(new Address("Apfel", "Anton")); addresses.add(new Address("Hüpfer", "Hugo")); addresses.add(new Address("Frosch", "Frodo")); addresses.add(new Address("Kaninchen", "Sabinchen")); addresses.add(new Address("Topfdeckel", "Teo")); addresses.add(new Address("Traurig", "Trude")); addresses.add(new Address("Warmer", "Werner")); } public Object[] getAddesses() { return addresses.toArray(new Address[0]); } } class Address { private String strLastName; private String strFirstName; public Address(String strLastName, String strFirstName) { this.strLastName = strLastName; this.strFirstName = strFirstName; } public void setLastName(String strLastName) { this.strLastName = strLastName; } public String getLastName() { return strLastName; } public void setFirstName(String strFirstName) { this.strFirstName = strFirstName; } public String getFirstName() { return strFirstName; } }
Flags für die Tabellenerstellung:
SWT.SINGLE - Es kann immer eine Tabellenzeile zur Zeit selektiert werden
SWT.MULTI - Es können mehrere Tabellenzeilen gleichzeitig selektiert sein
SWT.CHECK - Plaziert eine Check-Box am Anfang jeder Zeile
SWT.FULL_SELECTION - Die ganze selektierte Zeile wird eingefärbt
SWT.HIDE_SELECTION - Selektierte Zeilen/Zellen werden nicht eingefärbt