SWT Scrollbar events on Linux

2019-07-29 11:23发布

问题:

As asked here Scrolling part of the canvas I am not able to listen the mouse events on scrollbars. After investigation I found that it is due to a bug in GTK. See here https://bugs.eclipse.org/bugs/show_bug.cgi?id=51995. It is fixed now but I don’t know how to resolve it on my machine(Ubuntu 12.04). Can any body help me in this regard?

回答1:

Either get the source, apply the patch or build from git via your distributions package building facitily (which in your case will be a pain, debian packaging requires you to jump through bunch of loops) or find a PPA (read: find somebody else who did that work already) and install it (do that at your own risk)



回答2:

You can detect the mouse events on scrollbar or slider as mentioned below.

Set the page increment and increment values to Slider or Scrollbar.

sbr.setPageIncrement(100); //Scrollbar will be moved 100 pixel back or forth when clicked on area between thumb and left arrow button or area between thumb and right arrow button.

sbr.setIncrement(10); //Scrollbar will be moved 10 pixel back or forth when clicked on left or right arrow button.

Add the below code in scrollbar or slider selection listener

sbr.addListener(SWT.Selection, new Listener() {
  @Override
  public void handleEvent(Event event) {
      int hSelection = sbr.getSelection();

      if (hSelection - prevHselection == sbr.getIncrement()) {
         System.out.println("clicked right arrow button");
      } else if (hSelection - prevHselection == -sbr.getIncrement()) {
         System.out.println("clicked left arrow button");
      } else if (hSelection - prevHselection == sbr.getPageIncrement()) {
         System.out.println("clicked on area between thumb and right arrow button");              
      } else if (hSelection - prevHselection == -sbr.getPageIncrement()) 
         System.out.println("clicked on area between thumb and left arrow button");   
      } else if(hSelection - prevHselection > 0){
         System.out.println("Thumb is dragged forward");                 
      } else if(hSelection - prevHselection < 0){
         System.out.println("Thumb is dragged backward");                 
      }

      prevHselection = hSelection; //create field prevSelection
  }
}

Note AFTER client resize update page increment and increment values.