网页
综合
更新时间 2026-11-13 17:50:48
Virtual Tree and Table in SWT
自动蜘蛛
www.eclipse.org
正文约 6000 字
Summary Virtual Tables and Trees allow developers to quickly create Tables and Trees with large amounts of data and populate them efficiently. This article is an overview of how to use virtual Tables and Trees within SWT applications.
By Beatriz Iaderoza and Grant Gayed, IBM Ottawa Lab June 5th, 2006
When creating a Table or Tree in an SWT application, each item (row) is represented by a TableItem or TreeItem. The creation of these items can be time consuming if there is a very large number of them, or if computing the data they require is expensive. This can pose a problem in traditional Tables and Trees because their items all get created up-front, which can make initial population prohibitively slow. Additionally, if the user does not view all of the created items, as is very often the case with large Tables and Trees, then much of this initial cost is wasted.
Virtual Tables and Trees are ideal for handling situations like this. In virtual Tables and Trees, items are created on an on-demand basis. This ensures that no extra effort or memory footprint is spent creating items that are never viewed, and it spreads out the Table or Tree population effort over the lifetime of the control instead of being completely up-front.
One example scenario where a virtual Table would be useful is for displaying the results of a search query on a library book database. This could exhibit a slow initial population time with a non-virtual Table because of the potential for huge result sets and expensive database queries required to retrieve item summary information. However, a virtual table should perform well here since the initial population time would just be the time required to create the first page of items. In a system with good result ranking heuristics these could prove to be the only items ultimately viewed by the user.
The following sections describe how to use virtual Tables and Trees, and provide example code snippets with explanations. It should be noted that the Virtual Table section contains concepts that are applicable to virtual Trees as well.
In order to populate a virtual Table, you must specify the number of items it will contain and an SWT.SetData listener that will set data on provided items. The item count is used by the Table to set its vertical scrollbar's maximum and thumb values to reflect a table of this size, and can be changed throughout its lifetime.
When the data for an uninitialized item is required, often because the item became visible to the user for the first time, the item is automatically created and passed to the SWT.SetData listener to have its data set. The item data consists of all of the item's attributes (texts, images, colors and checked state). The operating system is free to request the data for any item in any order. Once an item has been initialized, either by a SWT.SetData listener or by TableItem API such as setText(), then its value becomes permanent and the SWT.SetData listener is not called to set its data again. The only exception to this occurs if an item is cleared with Table clear() API, in which case the listener will be called to set its data again as necessary.
Let us walk through a simple example that shows the basic concepts involved in creating and using a virtual Table. The example in Listing 1 demonstrates the creation of a Table with 10000 items, and the modification of an item that has already had its data set:
Listing 1: Adding 10000 items to a virtual Table, and arbitrarily clearing the fifth item.
Lines 2-5: Creates the strings that will be used to set the item data in the example's Table.
Line 6: Creates the Table. In order to create a virtual Table, SWT.VIRTUAL is specified in its constructor's style argument. Note that if you have other styles for the Table, such as BORDER, then the styles are OR'd together as shown.
Lines 7 and 8: Adds a SWT.SetData listener to the Table. This is the listener that is responsible for setting the data on the item in the event's item field whenever invoked.
Line 9: Gets the TableItem that is to have its data set.
Lines 10 and 11: Gets the index of the item and sets its text accordingly. The index of this item can be found in the event's index field (@since 3.2). Alternatively, this index can be computed with Table.indexOf(TableItem).
Line 14: Sets the item count of the Table to 10000, indicating that it should be able to hold 10000 items.
Lines 15 and 16: Creates a Button that will trigger a value change in the Table's fifth item.
Lines 19 and 20: Handles a press of the button by putting a new value into index 5 of the itemStrings array, and clearing index 5 in the Table. If this item subsequently becomes visible to the user then the SWT.SetData listener will be invoked with it as the item, and the itemStrings array's new value will be set into it.
In addition to the described SWT.SetData listener, data can still be set on uninitialized items in a virtual Table with typical TableItem APIs such as setText(...), setImage(...), etc. Table.getItem(int) can be used to get a TableItem that has not yet been initialized but is within the Table's specified item count. An application that is retrieving records from an external source, such as the presented library database scenario, may benefit from selectively setting data on some items before they are requested by the SWT.SetData listener. For example, if there is a high cost associated with establishing each database connection, then retrieving a chunk of records whenever a query is required can yield better results than initiating separate connections for each item that is requested. Listing 2 demonstrates this approach using a retrieval block size of 64 records.
Listing 2: Loading table items from a database in blocks of 64 records.
Line 3: Creates the virtual Table by specifying style SWT.VIRTUAL.
Lines 4 and 5: Adds a SWT.SetData listener to the Table, which is responsible for setting the data on the item in the event's item field whenever invoked.
L
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。