1

Currently with help of JAVA SWT (org.eclipse.swt.widgets.Table & org.eclipse.jface.viewers.TableViewer library), I have a table which is getting populated from DB. Idea is to refresh the table data while user is scroll down to check the tables row using scroll-bar and let the scroll bar position restore to same point where it was before refresh event. So that user never feel that some refresh event is working in background while he scroll down the data.

Is there any inbuilt method or library exist in jdk or in above eclipse package to get & set the position of scroll-bar dynamically?

I found a logic to get scroll bar position

ScrollBar vScroll = Table.getVerticalBar();
        int valuetemp = vScroll.getSelection();

But after refreshing the table content, when scroll-bar moved to very fist entry after that if I use below code to restore the scroll-bar. Ideally it should work as per API but it didn't move my scroll-bar position to the previous value.

vScroll.setSelection(valuetemp);
  • Possible duplicate of [Using native functions in Android with OpenCV](http://stackoverflow.com/questions/853020/jtable-scrolling-to-a-specified-row-index) – Fady Saad Apr 24 '17 at 06:21
  • 1
    @user7790438 No I'm not using jtable, its a org.eclipse.swt.widgets.Table in which there is no such inbuilt method available. – Sanjay Mishra Apr 24 '17 at 06:29
  • If you are using JFace's TableViewer you should not use the underlying Table directly. TableViewer::refresh is able to restore the selection and scroll position in most cases. Please post your refresh code. – Rüdiger Herrmann Apr 24 '17 at 16:40
  • Actually there is user defined refresh method which getting refresh after every few seconds. Internally they call different methods which actually clean the table content and get populate the table from the DB again. Before cleaning they store value of last selected row and after getting update from DB they use this value to restore the selection to selected row. Problem is that once it restore the selected row, scroll-bar control moves immediately to that records. Which cause the problem, because when user try to navigate the table using scroll-bar it moves back to selected row. – Sanjay Mishra Apr 25 '17 at 06:16
  • With vScroll.getSelection() & vScroll.setSelection(valuetemp) method I verified that scrollbar value is actually change but their impact is not seen on scrollbar movement. That means even changing the value of scrollbar to the previous value, its not visible on scrollbar position, which remain at the selected row instead of new scroll value – Sanjay Mishra Apr 25 '17 at 06:29
  • Is there any way to get scrollbar moves to the scrollbar value? – Sanjay Mishra Apr 25 '17 at 06:31
  • With TableViewer you should use `setSelection` to restore the previous selection, don't try to mess with the scroll bars directly. – greg-449 Apr 25 '17 at 06:38
  • @greg-449 yes with setSelection method it restore the value as i confirm in logs but its scrollbar not moved to that value... as it should be that's the problem now – Sanjay Mishra Apr 27 '17 at 05:38
  • `setSelection` with `true` for the `reveal` argument should set the scroll bars appropriately. – greg-449 Apr 27 '17 at 06:37
  • @greg-449 sorry i didn't get you. Kindly find my further comment to continue this message...What i had done is: `code` Table myTable; ScrollBar vScroll; .... Logic to get selected index from the table .... vScroll = myTable.getVerticalBar(); int valuetemp = vScroll.getSelection(); // store current scrollbar position where exactly the user nevigate ... Logic to clear content of table and re fill the table / referesh table data from DB ... Logic to restore table selection, Due to this scrollbar move to the last selected value as its restored. `code` – Sanjay Mishra Apr 27 '17 at 10:00
  • ... Now vScroll = myTable.getVerticalBar(); vScroll.setSelection(valuetemp); // to restore scrollbar position So, far this is done and their impact is seen on scrollbar position but scrollbar remain stay where selection is restored. I don't know what 'true' value you are talking about for which reveal argument, as setSelection accept only single integer value. Can you further explain? – Sanjay Mishra Apr 27 '17 at 10:01
  • `TableViewer` `setSelection` has two arguments. If you use `TableViewer` you should **not** do anything with the `Table` it contains as the viewer is in control of the table. – greg-449 Apr 27 '17 at 10:04
  • I tried with TableViewer but it clear the selected item from the table even after restoring it. My requirement is that selected item should remain selected and scrollbar position should be also restore to the same point as it was previously scrolled by user instead to the selected item. – Sanjay Mishra Apr 27 '17 at 10:29

1 Answers1

1

I had a similar problem. I tried to keep the scrollbar at the newest table input. I did not found a solution for the

    ScrollBar.class

but the following code worked for me:

    table.setTopIndex(table.getItemCount()-1);

I hope this helps :)

Julian
  • 334
  • 4
  • 18