Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-swt-dev] Table examples collected from the newsgroup

Hi dear list subscribers

I am at the beginning to study SWT/JFace. As the Table/Tree are the more
complex widgets and the one I need in business applications, I choosed to
start with Table and the compound viewer.
I soon noticed that there are serveral limitations compared to JTable (no
backgroundcolor, no checkboxes or buttons as renderes, no keyboard
handling). 
But I found some encouraging examples in the newsgroup, especially from
Veronica Irvine. As I think it could fit to the snippets section at the
swt-dev-site, I attached them. 
They are more like blueprints, but I hope I can learn
enough from them to implement background coloring for certain cells as a
first goal.

martin
package swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

/**Table Keyboard handling*/
public class TableExample {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new org.eclipse.swt.layout.GridLayout());
		final Color red = display.getSystemColor(SWT.COLOR_RED);

		final Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		TableColumn column1 = new TableColumn(table, SWT.NONE);
		TableColumn column2 = new TableColumn(table, SWT.NONE);
		TableColumn column3 = new TableColumn(table, SWT.NONE);
		for (int i = 0; i < 100; i++) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(
				new String[] {
					"cell " + i + " 0",
					"cell " + i + " 1",
					"cell " + i + "2" });
		}
		column1.pack();
		column2.pack();
		column3.pack();

		final TableCursor cursor = new TableCursor(table, SWT.NONE);
		final ControlEditor editor = new ControlEditor(cursor);
		editor.grabHorizontal = true;
		editor.grabVertical = true;
		cursor.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				table.setSelection(new TableItem[] { cursor.getRow()});
			}
			public void widgetDefaultSelected(SelectionEvent e) {
				final Text text = new Text(cursor, SWT.NONE);
				TableItem row = cursor.getRow();
				int column = cursor.getColumn();
				text.setText(row.getText(column));
				text.setBackground(red);
				text.addTraverseListener(new TraverseListener() {
					public void keyTraversed(TraverseEvent e) {
						if (e.detail == SWT.TRAVERSE_ESCAPE) {
							TableItem row = cursor.getRow();
							int column = cursor.getColumn();
							row.setText(column, text.getText());
							text.dispose();
							e.doit = false;
						}

					}
				});
				editor.setEditor(text);
				text.setFocus();
			}
		});

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

/**Editor second column*/
public class TableExample1 {
	Shell shell;
	Table table;
	Text text;
	Font font;
	TableEditor editor;
	static String[][] data;
	static {
		int count = 64;
		data = new String[count][2];
		for (int i = 0; i < count; i++) {
			data[i] = new String[] { "Item" + i, "Data" + i };
		}
	};
	void close() {
		if (shell != null && !shell.isDisposed())
			shell.dispose();
		shell = null;
		table = null;
		if (font != null)
			font.dispose();
		font = null;
	}
	public static void main(String[] args) {
		TableExample1 window = new TableExample1();
		window.open();
		window.run();
		window.close();
	}
	void open() {
		shell = new Shell();
		shell.setText("Table Test");
		shell.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event e) {
				resizeShell(e);
			}
		});

		int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL;
		table =
			new Table(shell, style | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
		table.setFont(font);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		TableColumn column1 = new TableColumn(table, SWT.NONE);
		column1.setText("Type");
		column1.setWidth(100);
		TableColumn column2 = new TableColumn(table, SWT.NONE);
		column2.setText("Value");
		column2.setWidth(100);
		for (int i = 0; i < data.length; i++) {
			TableItem item = new TableItem(table, 0);
			item.setText(data[i]);
		}
		table.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				selectTable(e);
			}
		});
		editor = new TableEditor(table);
		shell.open();
	}
	void resizeShell(Event e) {
		table.setBounds(shell.getClientArea());
	}
	void run() {
		Display display = shell.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
	void selectTable(Event e) {
		TableItem oldItem = editor.getItem();
		editor.setEditor(null, null, -1);
		if (oldItem != null) {
			int row = table.indexOf(oldItem);
			data[row][1] = text.getText();
			oldItem.setText(1, data[row][1]);
			text.dispose();
		}
		int index = table.getSelectionIndex();
		if (index == -1)
			return;
		TableItem newItem = table.getItem(index);
		if (newItem == oldItem)
			return;
		table.showSelection();
		text = new Text(table, SWT.SINGLE);
		text.setFont(table.getFont());
		text.setText(data[index][1]);
		text.selectAll();
		editor.horizontalAlignment = SWT.LEFT;
		editor.grabHorizontal = true;
		editor.minimumWidth = 50;
		editor.setEditor(text, newItem, 1);
		text.setFocus();
	}
}
package swt.test;

import java.io.File;
import java.text.DateFormat;
import java.util.Date;

import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/** Show files, render checkbox */
public class TableExample10 {

	public static void main(String[] args) {
		new TableExample10().run();
	}

	void run() {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setSize(600, 500);

		TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
		TabItem tabItem1 = new TabItem(tabFolder, SWT.NONE);
		tabItem1.setText("Tab 1");
		TabItem tabItem2 = new TabItem(tabFolder, SWT.NONE);
		tabItem2.setText("Tab 2");
		TabItem tabItem3 = new TabItem(tabFolder, SWT.NONE);
		tabItem3.setText("Tab 3");

		Table table =
			new Table(
				tabFolder,
				SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		tabItem1.setControl(table);

		TableColumn col1 = new TableColumn(table, SWT.NONE);
		col1.setText("Name");
		TableColumn col2 = new TableColumn(table, SWT.NONE);
		col2.setText("Size");
		TableColumn col3 = new TableColumn(table, SWT.NONE);
		col3.setText("Date");

		TableLayout layout = new TableLayout();
		layout.addColumnData(new ColumnWeightData(33));
		layout.addColumnData(new ColumnWeightData(33));
		layout.addColumnData(new ColumnWeightData(33));
		table.setLayout(layout);

		final CheckboxTableViewer viewer = new CheckboxTableViewer(table);
		viewer.setContentProvider(new TestContentProvider());
		viewer.setLabelProvider(new TestLabelProvider());
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection sel =
					(IStructuredSelection) event.getSelection();
				System.out.println(
					sel.size()
						+ " items selected, "
						+ viewer.getCheckedElements().length
						+ " items checked");
			}
		});
		viewer.setInput(new File("/"));

		Label dummy1 = new Label(tabFolder, SWT.NONE);
		dummy1.setText("Nothing here.");
		tabItem2.setControl(dummy1);
		Label dummy2 = new Label(tabFolder, SWT.NONE);
		dummy2.setText("Nothing here either.");
		tabItem3.setControl(dummy2);

		shell.setVisible(true);
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	class TestContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object input) {
			if (input instanceof File) {
				File dir = (File) input;
				String[] names = dir.list();
				if (names != null) {
					File[] files = new File[names.length];
					for (int i = 0; i < names.length; i++)
						files[i] = new File(dir, names[i]);
					return files;
				}
			}
			return new File[0];
		}
		public void inputChanged(
			Viewer viewer,
			Object oldInput,
			Object newInput) {
			// don't need to hang onto input for this example, so do nothing
		}
		public void dispose() {
		}
	}

	class TestLabelProvider
		extends LabelProvider
		implements ITableLabelProvider {
		DateFormat dateFormat = DateFormat.getInstance();
		public String getColumnText(Object element, int columnIndex) {
			File file = (File) element;
			switch (columnIndex) {
				case 0 :
					return file.getName();
				case 1 :
					return file.isDirectory()
						? ""
						: ((file.length() + 1023) / 1024) + " KB ";

				case 2 :
					Date date = new Date(file.lastModified());
					return dateFormat.format(date);
				default :
					return "";
			}
		}
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
	}
}
package swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

/**Change background color*/
public class TableExample2 {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		Table table = new Table(shell, SWT.BORDER);
		table.setLinesVisible(true);
		TableColumn column1 = new TableColumn(table, SWT.NONE);
		TableColumn column2 = new TableColumn(table, SWT.NONE);
		for (int i = 0; i < 20; i++) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(
				new String[] { "Cell " + i + ", 0", "Cell " + i + ", 1" });
		}
		TableItem[] items = table.getItems();
		Color gray = display.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW);
		for (int i = 0; i < items.length; i = i + 2) {
			TableEditor editor0 = new TableEditor(table);

			Text text0 = new Text(table, SWT.READ_ONLY);
			text0.setText(items[i].getText(0));
			text0.setBackground(gray);
			editor0.setEditor(text0, items[i], 0);
			editor0.grabHorizontal = true;

			TableEditor editor1 = new TableEditor(table);
			Text text1 = new Text(table, SWT.READ_ONLY);
			text1.setText(items[i].getText(1));
			text1.setBackground(gray);
			editor1.setEditor(text1, items[i], 1);
			editor1.grabHorizontal = true;
		}
		table.setLocation(10, 10);
		column1.pack();
		column2.pack();
		table.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}
package swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**custom rendering */
public class TableExample3 {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		final Table table = new Table(shell, SWT.BORDER);
		table.setLinesVisible(true);
		TableColumn column1 = new TableColumn(table, SWT.NONE);
		column1.setText("[header 1]");
		TableColumn column2 = new TableColumn(table, SWT.NONE);
		column2.setText("[header 2]");
		for (int i = 0; i < 30; i++) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(0, "item " + i);
		}
		column1.pack();
		column2.setWidth(300);
		TableItem[] items = table.getItems();
		final Color blue = display.getSystemColor(SWT.COLOR_DARK_BLUE);
		final Color white = display.getSystemColor(SWT.COLOR_WHITE);
		for (int i = 0; i < items.length; i++) {
			TableItem tableItem = items[i];
			TableEditor editor = new TableEditor(table);
			editor.grabHorizontal = true;
			editor.grabVertical = true;
			final Canvas canvas = new Canvas(table, SWT.NONE);
			canvas.setData("EXAMPLE DATA", new Integer(i * 100 / items.length));
			canvas.addPaintListener(new PaintListener() {
				public void paintControl(PaintEvent e) {
					Rectangle area = canvas.getClientArea();
					Integer data = (Integer) canvas.getData("EXAMPLE DATA");
					if (data == null)
						return;
					e.gc.setBackground(table.getBackground());
					e.gc.fillRectangle(area.x, area.y, area.width, area.height);
					e.gc.setBackground(blue);
					e.gc.setForeground(white);
					e.gc.fillGradientRectangle(
						area.x,
						area.y,
						(int) (data.doubleValue() * area.width / 100.0),
						area.height,
						false);
				}
			});
			editor.setEditor(canvas, tableItem, 1);
		}
		table.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}
package swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/** detect row/column with Mouse.DOWN */
public class TableExample4 {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		final Table table =
			new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		final int rowCount = 64, columnCount = 4;
		for (int i = 0; i < columnCount; i++) {
			TableColumn column = new TableColumn(table, SWT.NULL);
			column.setText("Column " + i);
		}
		for (int i = 0; i < rowCount; i++) {
			TableItem item = new TableItem(table, SWT.NULL);
			for (int j = 0; j < columnCount; j++) {
				item.setText(j, "Item " + i + "-" + j);
			}
		}
		for (int i = 0; i < columnCount; i++) {
			table.getColumn(i).pack();
		}
		Point size = table.computeSize(SWT.DEFAULT, 200);
		table.setSize(size);
		shell.pack();
		table.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				Point pt = new Point(event.x, event.y);
				TableItem item = table.getItem(pt);
				if (item == null)
					return;
				for (int i = 0; i < columnCount; i++) {
					Rectangle rect = item.getBounds(i);
					if (rect.contains(pt)) {
						int index = table.indexOf(item);
						System.out.println("Item " + index + "-" + i);
					}
				}
			}
		});
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}
package swt.test;

import java.util.Arrays;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColorCellEditor;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Widget;

/**Different editors in action*/
public class TableExample5 {

	static Button create;
	static String[] listComb = new String[] { "vert", "bleu", "rouge" };

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell();
		shell.setSize(400, 400);
		shell.setLayout(new FillLayout());

		DummyElement[] datas =
			new DummyElement[] {
				new DummyElement(new RGB(255, 12, 40), "row1col2", listComb[0]),
				new DummyElement(
					new RGB(70, 255, 40),
					"row2col2",
					listComb[0])};

		create = new Button(shell, SWT.PUSH);
		create.setText("Create New");

		Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		TableLayout tableLayout = new TableLayout();
		tableLayout.addColumnData(new ColumnWeightData(1, 50, true));
		tableLayout.addColumnData(new ColumnWeightData(1, 50, true));
		tableLayout.addColumnData(new ColumnWeightData(1, 50, true));
		table.setLayout(tableLayout);
		new TableColumn(table, SWT.LEFT).setText("col1");
		new TableColumn(table, SWT.NONE).setText("col2");
		new TableColumn(table, SWT.RIGHT).setText("col3");
		final TableViewer tableViewer = new TableViewer(table);
		tableViewer.setContentProvider(new DummyContentProvider());
		tableViewer.setLabelProvider(new DummyLabelProvider(table));
		
		ITableLabelProvider labelProvider=new ITableLabelProvider() {
			Label label= new Label(tableViewer.getTable(),SWT.NORMAL);
			public Image getColumnImage(Object element, int columnIndex) {
				label.setText(element.toString()+"Test ");
				return label.getImage();
			}

			public String getColumnText(Object element, int columnIndex) {
				return null;
			}

			public void addListener(ILabelProviderListener listener) {
				System.out.println("");
			}

			public void dispose() {
				label.dispose();
			}

			public boolean isLabelProperty(Object element, String property) {
				return false;
			}

			public void removeListener(ILabelProviderListener listener) {
				System.out.println("");
			}
		};
		
		//tableViewer.setLabelProvider(labelProvider);
		
		CellEditor editor=new ColorCellEditor(table);
		editor.getControl().setSize(50, 50);
		tableViewer.setCellEditors(
			new CellEditor[] {
				editor,
				new TextCellEditor(table),
				new ComboBoxCellEditor(table, listComb)});
		create.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				tableViewer.getTable().removeAll();
			}
		});

		tableViewer.setCellModifier(new ICellModifier() {
			public boolean canModify(Object element, String property) {
				return true;
			}

			public Object getValue(Object element, String property) {
				DummyElement e = (DummyElement) element;
				if (property == "col1")
					return e.col1;
				else if (property == "col2")
					return e.col2;
				else if (property == "col3") {
					int i = Arrays.asList(listComb).indexOf(e.col3);
					return i == -1 ? null : new Integer(i);
				} else
					return null;
			}

			public void modify(Object element, String property, Object value) {
				// workaround for bug 1938 where element is Item rather than model element 
				if (element instanceof Item)
					element = ((Item) element).getData();

				DummyElement e = (DummyElement) element;

				if (property == "col1")
					e.col1 = (RGB) value;
				else if (property == "col2")
					e.col2 = (String) value;
				else if (property == "col3")
					if (value instanceof Integer) {
						int i = ((Integer) value).intValue();
						e.col3 = listComb[i];
					}

				// This is a hack. Changing the model above should cause it to notify
				// the content provider, which should update the viewer.
				// It should not be done directly here.
				tableViewer.update(e, null);
			}
		});

		tableViewer.setColumnProperties(
			new String[] { "col1", "col2", "col3" });

		tableViewer.setInput(datas);

		shell.open();

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

		}

		display.dispose();

	}

	public static class DummyElement {
		public RGB col1;
		public String col2;
		public String col3;
		public DummyElement(RGB col1, String col2, String col3) {
			this.col1 = col1;
			this.col2 = col2;
			this.col3 = col3;
		}
	}

	public static class DummyLabelProvider
		extends LabelProvider
		implements ITableLabelProvider {

		Widget fWidget=null;

		public DummyLabelProvider(Widget parentWidget){
			fWidget=parentWidget;
		}

		public void dispose() {
			if(fWidget!=null)
				fWidget.dispose();
		}


		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			String columnText = null;
			DummyElement dummyElement = (DummyElement) element;
			switch (columnIndex) {
				case 0 :
					columnText = "" + dummyElement.col1;
					break;
				case 1 :
					columnText = "" + dummyElement.col2;
					break;
				case 2 :
					columnText = "" + dummyElement.col3;
					break;
			}
			return columnText;

		}

	}

	public static class DummyContentProvider
		implements IStructuredContentProvider {

		public Object[] getElements(Object input) {
			return (DummyElement[]) input;
		}

		public void dispose() {
		}

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

			// should hook a listener on the model here (newInput)
		}

	}
}
package swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**Hide/show headers, modify headers*/
public class TableExample6 {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());
		final Table table = new Table(shell, SWT.BORDER);
		final TableColumn column1 = new TableColumn(table, SWT.NONE);
		column1.setText("Column 1");
		final TableColumn column2 = new TableColumn(table, SWT.NONE);
		column2.setText("Column 2");
		for (int i = 0; i < 10; i++) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(new String[] { "table item " + i, "more stuff" });
		}
		table.setLayoutData(new GridData(GridData.FILL_BOTH));
		column1.pack();
		column2.pack();

		Button b = new Button(shell, SWT.PUSH);
		b.setText("Show/Hide headers");
		b.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				boolean visible = table.getHeaderVisible();
				table.setHeaderVisible(!visible);
			}
		});

		b = new Button(shell, SWT.PUSH);
		b.setText("Modify headers");
		b.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				column1.setText(column1.getText() + " modified");
				column1.pack();
				column2.setText(column2.getText() + " modified");
				column2.pack();
			}
		});

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

}
package swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/** ??? */
public class TableExample7 {

	public static void main(String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		final Table table = new Table(shell, 0);
		table.setHeaderVisible(true);
		final int columns = 4;
		for (int i = 0; i < columns; i++) {
			new TableColumn(table, 0);
		}
		final int rows = 12;
		for (int i = 0; i < rows; i++) {
			TableItem item = new TableItem(table, 0);
			for (int j = 0; j < columns; j++) {
				item.setText(j, "Item " + i + "-" + j);
			}
		}
		shell.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event e) {
				table.setRedraw(false);
				table.setBounds(shell.getClientArea());
				Rectangle rect = table.getClientArea();
				for (int i = 0; i < columns; i++) {
					TableColumn column = table.getColumn(i);
					column.setWidth(rect.width / columns);
				}
				table.setRedraw(true);
				Rectangle rect2 = table.getClientArea();
				if (!rect.equals(rect2)) {
					table.setRedraw(false);
					rect.width = rect2.width;
					for (int i = 0; i < columns; i++) {
						TableColumn column = table.getColumn(i);
						column.setWidth(rect.width / columns);
					}
					table.setRedraw(true);
					table.update();
				}
			}
		});
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

}
package swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * Here is a wad of code to show how to resize the table columns only on initialization. 
 * Note, the size of the table must be determined somehow.  We have used GridData.FILL_BOTH 
 * as the means of setting the size of the table, you may have some other way. 
 */
public class TableExample8 {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());
		final Table table = new Table(shell, SWT.BORDER);
		table.setLayoutData(new GridData(GridData.FILL_BOTH));
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		final int columnCount = 4;
		for (int i = 0; i < columnCount; i++) {
			TableColumn column = new TableColumn(table, SWT.NONE);
			column.setText("Column " + i);
		}

		final Listener[] listener = new Listener[1];
		listener[0] = new Listener() {
			public void handleEvent(Event e) {
				table.removeListener(SWT.Resize, listener[0]);
				Rectangle rect = table.getClientArea();
				TableColumn[] columns = table.getColumns();
				table.setRedraw(false);
				for (int i = 0; i < columns.length; i++) {
					columns[i].setWidth(rect.width / columns.length);
				}
				table.setRedraw(true);
				// bug in table, redraw should not be neccessary 
				table.redraw();
			}
		};
		table.addListener(SWT.Resize, listener[0]);

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

}
package swt.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/** Row/Column wich get clicked  */
public class TableExample9 {
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		final Table table =
			new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		final int rowCount = 64, columnCount = 4;
		for (int i = 0; i < columnCount; i++) {
			TableColumn column = new TableColumn(table, SWT.NULL);
			column.setText("Column " + i);
		}
		for (int i = 0; i < rowCount; i++) {
			TableItem item = new TableItem(table, SWT.NULL);
			for (int j = 0; j < columnCount; j++) {
				item.setText(j, "Item " + i + "-" + j);
			}
		}
		for (int i = 0; i < columnCount; i++) {
			table.getColumn(i).pack();
		}
		Point size = table.computeSize(SWT.DEFAULT, 200);
		table.setSize(size);
		shell.pack();
		table.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				Point pt = new Point(event.x, event.y);
				TableItem item = table.getItem(pt);
				if (item == null)
					return;
				for (int i = 0; i < columnCount; i++) {
					Rectangle rect = item.getBounds(i);
					if (rect.contains(pt)) {
						int index = table.indexOf(item);
						System.out.println("Item " + index + "-" + i);
					}
				}
			}
		});
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}

Back to the top