/*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.swt.snippets;
12
13 /*
14 * Virtual Table example snippet: create a table with 1,000,000 items (lazy)
15 *
16 * For a list of all SWT example snippets see
17 * http://www.eclipse.org/swt/snippets/
18 *
19 * @since 3.0
20 */
21 import org.eclipse.swt.*;
22 import org.eclipse.swt.widgets.*;
23 import org.eclipse.swt.layout.*;
24
25 public class Snippet144 {
26
27 static final int COUNT = 1000000;
28
29 public static void main(String[] args) {
30 Display display = new Display ();
31 final Shell shell = new Shell (display);
32 shell.setLayout (new RowLayout (SWT.VERTICAL));
33 final Table table = new Table (shell, SWT.VIRTUAL | SWT.BORDER);
34 table.addListener (SWT.SetData, new Listener () {
35 public void handleEvent (Event event) {
36 TableItem item = (TableItem) event.item;
37 int index = table.indexOf (item);
38 item.setText ("Item " + index);
39 System.out.println (item.getText ());
40 }
41 });
42 table.setLayoutData (new RowData (200, 200));
43 Button button = new Button (shell, SWT.PUSH);
44 button.setText ("Add Items");
45 final Label label = new Label(shell, SWT.NONE);
46 button.addListener (SWT.Selection, new Listener () {
47 public void handleEvent (Event event) {
48 long t1 = System.currentTimeMillis ();
49 table.setItemCount (COUNT);
50 long t2 = System.currentTimeMillis ();
51 label.setText ("Items: " + COUNT + ", Time: " + (t2 - t1) + " (ms)");
52 shell.layout ();
53 }
54 });
55 shell.pack ();
56 shell.open ();
57 while (!shell.isDisposed ()) {
58 if (!display.readAndDispatch ()) display.sleep ();
59 }
60 display.dispose ();
61 }
62 }
public class TableViewerVIRTUAL {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10,10,300,300);
Table table = new Table(shell, SWT.VIRTUAL);
table.setBounds(10,10,200,200);
final TableViewer viewer = new TableViewer(table);
viewer.setItemCount(10000); // <------
viewer.setLabelProvider(new ITableLabelProvider() {
public void removeListener(ILabelProviderListener listener) {}
public boolean isLabelProperty(Object element, String property)
{
return false;
}
public void dispose() {}
public void addListener(ILabelProviderListener listener) {}
public String getColumnText(Object element, int columnIndex) {
return "virtual item: " +
String.valueOf(((Integer)element).intValue());
}
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
});
viewer.setContentProvider(new ILazyContentProvider() { // <------
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {}
public void dispose() {}
public void updateElement(int index) {
System.out.println("setting: " + index);
viewer.replace(new Integer(index), index);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}