How to set Scroll bar thumb position programatically [message #876990] |
Fri, 25 May 2012 16:34 |
RaaZ Siripuram Messages: 3 Registered: April 2012 |
Junior Member |
|
|
I want to set scrollbar thumb position programatically based on some calculations.
For each mouse scroll event, arrow up/down,and page up/down event, I used
table.getVertcalBar().setSelection(selection);
to set thumb position.
It is working for mouse up/down scroll event and drag event.
But when I click on up/down arrows of verticalbar (and I click on vertical bar itself),
thumb position not placing at, where I had set.
Here is my code..
public class TableSample {
private static ScrollBar verticalBar;
private static Table table;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
createTable(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void createTable(Shell shell) {
table = new Table(shell, SWT.SINGLE);
TableColumn col1 = new TableColumn(table, SWT.LEFT);
col1.setText("Coloumn 1");
col1.setWidth(80);
TableColumn col2 = new TableColumn(table, SWT.LEFT);
col2.setText("Coloumn 2");
col2.setWidth(80);
for (int i = 0; i <= 999; i++) {
TableItem item1 = new TableItem(table, 0);
item1.setText(new String[] { "First" + i, "second" + i });
}
table.setHeaderVisible(true);
table.setLinesVisible(true);
verticalBar = table.getVerticalBar();
verticalBar.addSelectionListener(new ScrollBarHandler());
}
static class ScrollBarHandler extends SelectionAdapter {
public void widgetSelected(SelectionEvent ev) {
System.out.println("Scrollbar Event: " + ev.detail + "- #Lines: "
+ verticalBar.getThumb() + " - " + verticalBar.getThumb()
/ (table.getItemHeight() + table.getGridLineWidth()));
switch (ev.detail) {
case SWT.ARROW_UP:
System.out.println("Arrow Up");
setScrollPosition();
break;
case SWT.ARROW_DOWN:
System.out.println("Arrow Down");
setScrollPosition();
break;
case SWT.PAGE_UP:
System.out.println("Page Up");
setScrollPosition();
break;
case SWT.PAGE_DOWN:
System.out.println("Page Down");
setScrollPosition();
break;
case SWT.DRAG:
System.out.println("DRAG Event");
setScrollPosition();
break;
}
}
}
private static void setScrollPosition() {
table.getVerticalBar().setSelection(500);
}
}
I think,OS itself try to place thumb position to actual place, as Display.readAndDispatch() reads event from OS and dispatches internally.
Is there any fix for this.
|
|
|
Powered by
FUDForum. Page generated in 0.03979 seconds