Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 612057cdb032f789140714d74a787147530c2759 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Create a virtual table and add 1000 entries to it every 500 ms.
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import java.util.*;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet151 {

static int[] data = new int[0];

public static void main (String [] args) {
	final Display display = new Display ();
	Shell shell = new Shell (display);
	shell.setLayout(new FillLayout());
	final Table table = new Table(shell, SWT.BORDER | SWT.VIRTUAL);
	table.addListener(SWT.SetData, new Listener() {
		public void handleEvent(Event e) {
			TableItem item = (TableItem)e.item;
			int index = table.indexOf(item);
			item.setText("Item "+data[index]);
		}
	});
	Thread thread = new Thread() {
		public void run() {
			int count = 0;
			Random random = new Random();
			while (count++ < 500) {
				if (table.isDisposed()) return;
				// add 10 random numbers to array and sort
				int grow = 10;
				int[] newData = new int[data.length + grow];
				System.arraycopy(data, 0, newData, 0, data.length);
				int index = data.length;
				data = newData;
				for (int j = 0; j < grow; j++) {
					data[index++] = random.nextInt();
				}
				Arrays.sort(data);
				display.syncExec(new Runnable() {
					public void run() {
						if (table.isDisposed()) return;
						table.setItemCount(data.length);
						table.clearAll();
					}
				});
				try {Thread.sleep(500);} catch (Throwable t) {}
			}
		}
	};
	thread.start();
	shell.open ();
	while (!shell.isDisposed() || thread.isAlive()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}
}

Back to the top