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

标签 java swt eclipse-rcp

我想在 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/

相关文章:

Java - 无法加密 RDP 文件中的 Windows 远程桌面密码

java - Windows 状态栏中的应用程序图标

java - 如何将多种类型的用户重定向到各自的 Activity?

java - 从日历对象检索年月日期时出错

java - SWT - 我可以自定义绘制文本控件的背景吗?

java - 如何更改 SWT ExpandBar 的背景

java - 我可以阻止 RCP 编辑器加载吗

java - RCP p2 更新 : Only one of the following can be installed at once

java - Log4J 2 配置监控和按位比较

ubuntu - LIBDBUSMENU-GTK-CRITICAL 在 Ubuntu 11.10 下退出 RCP 应用程序