Populating huge table in SWT/JFace RCP application

后端 未结 3 1081
北荒
北荒 2021-02-04 11:39

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

相关标签:
3条回答
  • 2021-02-04 11:42

    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. Create the table with style SWT.VIRTUAL
    2. Set the row count using Table#setItemCount()
    3. Add a SWT.SetData Listener that fills the TableItems on demand.

    Here's a code snippet:

    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();
    }
    
    0 讨论(0)
  • 2021-02-04 11:56

    1 - use the setText(String[]) instead of setText(int, String) one call instead of several.

    2 - use myTable.setRedraw(false) before and myTable.setRedraw(true) after the process to stop all redrawing opérations during loading data.

    it's simpler and can improve performance !!

    good luck.

    on my side using this I load 2500 lines of 20 column in less than 300ms..... on a standard today PC.

    0 讨论(0)
  • 2021-02-04 12:06

    You want to do lazy-loading on the table display. Basically, you keep all these objects offscreen in memory, then create only a handful of actual GUI table rows. As the user scrolls you re-render these rows with the objects at that scroll location.

    See this article (EDIT: oops I meant this article) for a JFace example.

    0 讨论(0)
提交回复
热议问题