我想在 SWT 表中填充大量数据。在网上搜索后,我找到了下面的代码。但这段代码填充了硬编码数据。
public static void main( String[] args ) {
Display display = new Display();
Shell shell = new Shell( display );
shell.setLayout( new FillLayout() );
final Table table = new Table( shell, SWT.VIRTUAL );
table.setItemCount( 10000 );
table.addListener( SWT.SetData, new Listener() {
public void handleEvent( Event event ) {
TableItem item = (TableItem)event.item;
item.setText("Item" + table.indexOf( item ) ); // should be replaced
// by below for loop
}
} );
shell.setSize( 300, 500 );
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() ) {
display.sleep();
}
}
display.dispose();
}
我想在 table.addListener 句柄事件中填充如下所示的内容。请帮助我。提前致谢!
for (int i = 0; i < informationList.size(); i++) {
TableItem item = new TableItem(informationTable, SWT.CENTER);
item.setText(1, informationList.get(i).getName());
item.setText(2, "");
item.setText(3, "");
item.setText(4, ""+informationList.get(i).getId());
}
最佳答案
虚拟表通过监听器询问当前可见行的内容。这就是虚拟牌 table 的意义所在:“别调用我们,我们会调用你!”
table.addListener(SWT.SetData, new Listener() {
public void handleEvent( Event event ) {
TableItem item = (TableItem)event.item;
int index = table.indexOf(item);
item.setText(1, informationList.get(index).getName());
item.setText(2, "");
item.setText(3, "");
item.setText(4, "" + informationList.get(index).getId());
}
});
另请参阅Javadoc of the Table
widget which also contains an example (我突出显示):
Style
VIRTUAL
is used to create a Table whose TableItems are to be populated by the client on an on-demand basis instead of up-front. This can provide significant performance improvements for tables that are very large or for which TableItem population is expensive (for example, retrieving values from an external source).Here is an example of using a Table with style
VIRTUAL
:...
关于java - 在 SWT 小部件表中动态填充大量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54018518/