Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling SWT Table programmatically

Tags:

swt

How to do vertical scroll of SWT table programatically? I'm implementing search function on the table. When an item was found then it will be scrolled to the item found.

like image 256
kinclong2 Avatar asked Aug 20 '10 13:08

kinclong2


2 Answers

There are several methods you might want to try:

Table.showItem(TableItem)
Table.showSelection()
Table.showColumn(TableColumn)
Table.setTopIndex(int)

Other than that, I suggest using a TableViewer from JFace. Then you'd scroll to an item with this method:

TableViewer.reveal(Object)
like image 124
python dude Avatar answered Nov 13 '22 13:11

python dude


My full time job is to develop SWT (on Linux), I hope to be able to provide a comprehensive answer:

From a SWT code point of view (at least on on GTK), there are only 3 Table functions that affect scrolling via an internal native call gtk_tree_view_scroll_to_*()

setTopIndex();
showSelection();
showColumn();

To explain what they do & how to use them:

Vertical Scrolling

This is done by setting focus or selecting a particular table item.

For Table:

setTopIndex(int)      // Btw for Tree it's setTopItem(..)
showSelection()  // which is also reached via setSelection().

setTopIndex(int) moves the view programatically to the desired position.

Below is a modified version of [Snippet52][1] that performs the desired job:

public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell (display);
        Table table = new Table (shell, SWT.BORDER | SWT.MULTI);
        Rectangle clientArea = shell.getClientArea ();
        table.setBounds (clientArea.x, clientArea.y, 200, 200);
        for (int i=0; i<128; i++) {
            TableItem item = new TableItem (table, SWT.NONE);
            item.setText ("Item " + i);
        }
        table.setTopIndex(95);   // <<<< This is the interesting line.
        shell.pack ();
        shell.open ();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
 }

showSelection() on the other hand scrolls the view to the currently selected item. This method is also called by various setSelection(..) methods.

I.e setSelection(..) is typically used to scroll to the desired item and set keyboard focus on it. This is useful if you search for an item in a tree and would like user input (e.g 'enter') to act upon the item that you found. Snippet52 (mentioned above) performs this task.

Now it's worth noting that setSelection(..) doesn't trigger selectionListeners(...), so calling this method wouldn't invoke the associated action.

Horizontal Scrolling

This is done by focusing on a particular column via 'showColumn()'.

Below is a sample snippet that creates some rows & columns and then scrolls to the last column.

    public static void main (String [] args) {
          Display display = new Display ();
            Shell shell = new Shell (display);
            Table table = new Table (shell, SWT.BORDER | SWT.MULTI);
            table.setHeaderVisible (true);
            Rectangle clientArea = shell.getClientArea ();
            table.setBounds (clientArea.x, clientArea.y, 100, 100);
            String[] titles = {"First", "Second", "Third", "Fourth", "Fifth"};
            for (int i=0; i<titles.length; i++) {
                TableColumn column = new TableColumn (table, SWT.NONE);
                column.setText (titles [i]);
            }
            for (int i=0; i<128; i++) {
                TableItem item = new TableItem (table, SWT.NONE);
            item.setText (new String [] {"" + i, ""+i, ""+i, ""+i});
            }
            for (int i=0; i<titles.length; i++) {
                table.getColumn (i).pack ();
            }

            shell.pack ();
            shell.open ();

            display.asyncExec(
                    // Sometimes table column sizes are computed later at runtime,
                    // to get around it, set the column index after initialization.
                    () -> table.showColumn(table.getColumn(4))
            );
            while (!shell.isDisposed ()) {
                if (!display.readAndDispatch ()) display.sleep ();
            }
            display.dispose ();
        }

Note on Trees and Lists

Internally in SWT, Tree/Table/List all use the same native 'Tree' widget.

The above examples can be used for Lists and Tables as well, with the difference:

  • in Tree, setTopIndex(..) is setTopItem(..).
  • Lists don't have columns, so showColumn() is not applicable.

Let me know if you have further questions.

like image 45
Leo Ufimtsev Avatar answered Nov 13 '22 14:11

Leo Ufimtsev