简体   繁体   English

在SWT小部件表中动态填充大量数据

[英]Populate huge data dynamically in SWT widget table

I want to populate huge data in SWT Table. 我想在SWT表中填充大量数据。 After searching the internet, I found the below code. 搜索互联网后,我发现了以下代码。 But this code populates the hardcode data. 但是此代码将填充硬代码数据。

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();
}

I want to populate something like below inside the table.addListener handle event. 我想在table.addListener句柄事件内填充以下内容。 Please help me in this. 请帮助我。 Thanks in advance! 提前致谢!

 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());

 }

A virtual table asks via the listener for the contents of the currently visible rows. 一个虚拟表通过侦听器询问当前可见行的内容。 That's what the virtual table is all about: "Don't call us, we'll call you!" 这就是虚拟表的全部意义: “不要打电话给我们,我们会打电话给您!”

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());
    }
});

See also Javadoc of the Table widget which also contains an example (highlighting by me): 另请参阅Table小部件的Javadoc,它也包含一个示例 (我重点介绍):

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. VIRTUAL样式用于创建一个表,该表的TableItems将由客户端按需而不是预先填充 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). 对于很大的表或TableItem填充量很大的表(例如,从外部源检索值),这可以显着提高性能。

Here is an example of using a Table with style VIRTUAL : 这是一个使用样式为VIRTUAL的表的示例:

... ...

I achieved it by implementing it like 我通过实现它来实现它

    final String [] names = new String [informationList.size()];
    final int [] ids = new int [informationList.size()];
    table.setItemCount(informationList);
    for (int i = 0; i < informationList.size(); i++) {
        names [i] = informationList.get(i).getName();
    }
    for (int i = 0; i < informationList.size(); i++) {
        ids [i] = informationList.get(i).getId();
    }

    table.addListener(SWT.SetData, new Listener() {
        public void handleEvent(Event event) {
            TableItem item = (TableItem)event.item;
            int index = event.index;
            item.setText(1, names [index]);
            item.setText(2, "");
            item.setText(3, "");
            item.setText(4, ""+ids [index]);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM