Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to set Scroll bar thumb position programatically
How to set Scroll bar thumb position programatically [message #876990] Fri, 25 May 2012 16:34
RaaZ Siripuram is currently offline RaaZ SiripuramFriend
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. Embarrassed
Previous Topic:oleAutomation:unable to correctly call Visio API QueryCancelGroup
Next Topic:selection and deselection in treeviewer
Goto Forum:
  


Current Time: Fri Mar 29 09:15:52 GMT 2024

Powered by FUDForum. Page generated in 0.04038 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top