在SWT / JFace RCP应用程序中填充巨大的表 [英] Populating huge table in SWT/JFace RCP application

查看:131
本文介绍了在SWT / JFace RCP应用程序中填充巨大的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何在SWT表中显示大量行?巨大的是20K行,20列。不要问我为什么需要显示那么多数据,这不是重点。关键是如何让它尽可能快地工作,以便最终用户不会感到无聊等待。每行显示一些对象的实例,列是其属性(一些)。我想使用JFace内容/标签提供者模式,但担心它会比直接用数据命中表更慢。它是这样的:

How would you go about displaying huge amount of rows in SWT table? Huge is something above 20K rows, 20 columns. Don't ask me why I need to show that much data, it's not the point. The point is how to make it work as fast as possible so that end user won't get bored waiting. Each row displays an instance of some object, columns are its properties (some). I thought to use JFace content/label provider pattern, but afraid it will be even slower than hitting the table directly with the data. This is how it goes:

    Display.getDefault().asyncExec(new Runnable() {
       public void run() {
           List<MyObject> objects = model.getViewData();
           for(MyObject object: objects){
           TableItem item = new TableItem(table, SWT.NULL);
           item.setImage(0, img1);
           item.setBackground(color1);
               item.setText(0, object.getProperty0());
               item.setText(1, object.getProperty1());
               item.setText(2, object.getProperty2());
               ..... 
            }
       });

在我的电脑上绘制20k记录大约需要20秒。
我在Windows中看到的最大的性能问题是由新建表项创建和填充文本时由SWT发送的大量本机窗口消息引起的。我找到了很好的解决方法 - 在填充之前隐藏表,然后在完成后显示它。只需在循环之前调用table.setVisible(false),在循环之后调用table.setVisible(true)就可以了 - 速度上升了六七倍!

Drawing 20k records on my computer takes about 20 sec. The biggest performance problem I've seen in Windows are caused by incredible amount of native windowing messages sent by SWT when new table item created and populated with text. I've found great workaround for this - hide table before populating, and then show it when done. Just calling table.setVisible(false) before the loop and table.setVisible(true) after the loop does wonders - the speed goes up six-seven times!

我想加快速度。
你会建议什么?另外,我想知道隐藏小部件的这个技巧如何在SWT(又称Linux)的非Windows实现上起作用?

I'd like to speed it up even more. What would you suggest ? Also, I wonder how this trick hiding the widget would work on non-windows implementations of SWT (aka Linux) ?

推荐答案

SWT可以为您做到这一点。使用SWT.VIRTUAL样式标志时,仅在滚动到视图时创建项目。以下是如何操作:

SWT can do that for you. When you use the SWT.VIRTUAL style flag, items are only created when scrolled into view. Here's how to do it:


  1. 使用样式SWT.VIRTUAL创建表格

  2. 设置行使用表计数#setItemCount()

  3. 添加一个SWT.SetData监听器,按需填充TableItems。

这是一段代码片段:

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 ) );
        }
    } );
    shell.setSize( 300, 500 );
    shell.open();
    while( !shell.isDisposed() ) {
        if( !display.readAndDispatch() ) {
            display.sleep();
        }
    }
    display.dispose();
}

这篇关于在SWT / JFace RCP应用程序中填充巨大的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆