Scrolling SWT Table programmatically
Asked Answered
swt
W

3

14

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.

Wedged answered 20/8, 2010 at 13:9 Comment(0)
S
35

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)
Sachikosachs answered 20/8, 2010 at 13:34 Comment(2)
Yup. I found the solution myself digging last night in swt docs before reading your answer. BTW, thank's alot.Wedged
If this is the correct solution, please mark it as accepted so that other developers facing the same difficulty can see it as such.Baskerville
P
2

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.

Primary answered 23/3, 2018 at 16:25 Comment(6)
This will also select the item. OP didn't mention that this should happen. Also, there's already a detailed answer with 31 upvotes, I'm not sure another answer is required.Geoponics
@Geoponics I've done a number of SWT workshops. In my experience people/new comers often look for working snippets that they can adjust & copy. Linking to the snippet repo helps people find these snippets.Primary
It doesn't answer the question though, as the code you linked to also selects the item and doesn't just scroll to it.Geoponics
@Geoponics I've updated the answer and explained things in detail and provided some sample code. I'm a software engineer who works on SWT full time and was casually browsing around Stackoverflow to see what people struggle with. Let me know if you have any questions/feedback. Thanks.Primary
I'm not questioning your qualification here. All I was saying is that the answer wasn't quite on point and that "link-only" answers may be useful short-term, but loose all usefulness when the link goes down. What you posted now is a great answer!Geoponics
@Geoponics Nah, you were right. I was a bit lazy when I answered initially. It's good to have more complete answers.Primary
E
0

I don't really know what you need the search for, but you might also consider filtering the table to get to your desired element(kind of like a quick search).

Check it out: http://eclipsesource.com/blogs/2012/10/26/filtering-tables-in-swtjface/

Hope it helps, cheers!

Execrate answered 3/10, 2014 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.