Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to scroll a Canvas?
How to scroll a Canvas? [message #443180] Tue, 21 September 2004 11:13 Go to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
Hi all

Veronika has posted some code to the platform-swt-dev mailing list and
provided code that shows how to scroll a canvas. She added a key
listener to the canvas so that the canvas accepts focus.

However, I have still problems with getting the focus of the canvas, if
I have (see code below) a Button *AND* a Canvas. If I only have the
canvas, it is no problem to scroll (using the mouse wheel).

What I want is to be able in the code below to scroll up and down within
the canvas.

This works well if I select give focus to the text field or if I comment
out the following line in the main method:
//new Button(shell, SWT.PUSH).setText("Hello");
This makes the canvas the only composite within the shell.

I'd be very happy if anyone could help me on that. I tried to keep the
example of the form (code below) as small as possible, however, I still
wanted to provide working code.

Thanks in advance for any help on that.
Christian


/* FormCanvas.java */
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class FormCanvas extends Canvas {
private static final Rectangle FORM_SIZE = new Rectangle(0, 0, 500, 900);
private static final Point origin = new Point(0, 0);

private ScrollBar hBar;
private ScrollBar vBar;

private Label label;
private Rectangle labelBounds = new Rectangle(10, 53, 60, 20);
private Text text;
private Rectangle textBounds = new Rectangle(70, 50, 100, 20);

public FormCanvas(Composite parent) {
super(parent, SWT.H_SCROLL | SWT.V_SCROLL);

setBounds(FORM_SIZE);
addKeyListener(new KeyAdapter() {
}); // makes canvas accept focus (according to Veronika)
ScrollBarListener scrollBarListener = new ScrollBarListener(this);
hBar = getHorizontalBar();
hBar.addSelectionListener(scrollBarListener);
vBar = getVerticalBar();
vBar.addSelectionListener(scrollBarListener);
addListener(SWT.Resize, new FormResizeListener(this));
addPaintListener(new FormPaintListener());

// Add label
label = new Label(this, SWT.NONE);
label.setText("Your name:");
label.setBounds(labelBounds);
Color white = new Color(parent.getDisplay(), 0xEE, 0xEE, 0xEE);
label.setBackground(white);
white.dispose();

// Add text field
text = new Text(this, SWT.BORDER | SWT.SINGLE);
text.setBounds(textBounds);
}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());

new Button(shell, SWT.PUSH).setText("Hello");

Canvas canvas = new FormCanvas(shell);

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

/**
* Paint listener for the canvas.
*/
private class FormPaintListener implements PaintListener {
public void paintControl(PaintEvent event) {
Color white = new Color(event.display, 0xEE, 0xEE, 0xEE);
Color black = new Color(event.display, 0x00, 0x00, 0x00);
event.gc.setBackground(white);
event.gc.fillRectangle(origin.x, origin.y, FORM_SIZE.width - 1,
FORM_SIZE.height - 1);
event.gc.setForeground(black);
event.gc.drawRectangle(origin.x, origin.y, FORM_SIZE.width - 1,
FORM_SIZE.height - 1);
white.dispose();
black.dispose();
}
}

/**
* Listener for horizontal and vertical scroll bar.
*/
private class ScrollBarListener extends SelectionAdapter {
private Canvas canvas;

ScrollBarListener(Canvas canvas) {
this.canvas = canvas;
}

public void widgetSelected(SelectionEvent event) {
int hSelection = hBar.getSelection();
int vSelection = vBar.getSelection();
int destX = -hSelection - origin.x;
int destY = -vSelection - origin.y;
Rectangle rect = FORM_SIZE;
canvas.scroll(destX, destY, 0, 0, rect.width, rect.height, false);
origin.x = -hSelection;
origin.y = -vSelection;

// Reposition label and text
label.setBounds(origin.x + labelBounds.x, origin.y +
labelBounds.y, labelBounds.width, labelBounds.height);
text.setBounds(origin.x + textBounds.x, origin.y + textBounds.y,
textBounds.width, textBounds.height);
}
}

/**
* Listener that handles the form resize events.
*/
private class FormResizeListener implements Listener {
private Canvas canvas;

FormResizeListener(Canvas canvas) {
this.canvas = canvas;
}

public void handleEvent(Event event) {
Rectangle client = canvas.getClientArea();
hBar.setMaximum(FORM_SIZE.width);
vBar.setMaximum(FORM_SIZE.height);
hBar.setThumb(Math.min(FORM_SIZE.width, client.width));
vBar.setThumb(Math.min(FORM_SIZE.height, client.height));
int hPage = FORM_SIZE.width - client.width;
int vPage = FORM_SIZE.height - client.height;
int hSelection = hBar.getSelection();
int vSelection = vBar.getSelection();
if (hSelection >= hPage) {
if (hPage <= 0) {
hSelection = 0;
}
origin.x = -hSelection;
}
if (vSelection >= vPage) {
if (vPage <= 0) {
vSelection = 0;
}
origin.y = -vSelection;
}

// Reposition label and text
label.setBounds(origin.x + labelBounds.x, origin.y +
labelBounds.y, labelBounds.width, labelBounds.height);
text.setBounds(origin.x + textBounds.x, origin.y + textBounds.y,
textBounds.width, textBounds.height);

canvas.redraw();
}
}
}
Re: How to scroll a Canvas? [message #443209 is a reply to message #443180] Tue, 21 September 2004 17:32 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
If your canvas has children, then it is an enitrely different story.
Actually looking at your code, I recommend that you use a composite instead.

You need to detect the mouse down and set focus if it is appropriate. I
have modified your example to work below. Note that without the mouse down
listener, the code below still works if the focus is given to the text
widget. Also note that setting focus when a mouse down occurs in the blank
background of a composite is not expected platform behaviour.

I also modified your code to get rid of some unneccesdsary work and to stop
the flashing.


private static final Rectangle FORM_SIZE = new Rectangle(0, 0, 500, 900);

private static final Point origin = new Point(0, 0);

private static final Rectangle labelBounds = new Rectangle(10, 53, 60, 20);

private static final Rectangle textBounds = new Rectangle(70, 50, 100, 20);

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new Button(shell, SWT.PUSH).setText("Hello");
final Composite composite = new Composite(shell, SWT.H_SCROLL |
SWT.V_SCROLL | SWT.NO_BACKGROUND);
//canvas.setBounds(FORM_SIZE); // useless since you have a layout on shell
final ScrollBar hBar = composite.getHorizontalBar();
final ScrollBar vBar = composite.getVerticalBar();
// canvas.addKeyListener(new KeyAdapter() {
// makes canvas accept focus (according to Veronika)
// }); // not required because you have children
SelectionListener scrollBarListener = new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
int hSelection = hBar.getSelection();
int vSelection = vBar.getSelection();
// following lines not needed because composite already does this part
// int destX = -hSelection - origin.x;
// int destY = -vSelection - origin.y;
// Rectangle rect = FORM_SIZE;
// canvas.scroll(destX, destY, 0, 0, rect.width, rect.height, false);
origin.x = -hSelection;
origin.y = -vSelection;
composite.notifyListeners(SWT.Resize, new Event());
}
};
hBar.addSelectionListener(scrollBarListener);
vBar.addSelectionListener(scrollBarListener);

// this behaviour is not normal platform behaviour
// but it enable steh mousewheel to scroll when
// the user clicks anywhere in the composite
composite.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
Control focus = event.display.getFocusControl();
while (focus != null) {
if (focus == composite) return;
focus = focus.getParent();
}
composite.setFocus();
}
});

// Add label
final Label label = new Label(composite, SWT.NONE);
label.setText("Your name:");
label.setBounds(labelBounds);
Color white = display.getSystemColor(SWT.COLOR_WHITE);
label.setBackground(white);

// Add text field
final Text text = new Text(composite, SWT.BORDER | SWT.SINGLE);

final Text text2 = new Text(composite, SWT.BORDER | SWT.SINGLE);

// bounds setting for text and label not required because done by resize
listener
composite.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
Rectangle client = composite.getClientArea();
hBar.setMaximum(FORM_SIZE.width);
vBar.setMaximum(FORM_SIZE.height);
hBar.setThumb(Math.min(FORM_SIZE.width, client.width));
vBar.setThumb(Math.min(FORM_SIZE.height, client.height));
int hPage = FORM_SIZE.width - client.width;
int vPage = FORM_SIZE.height - client.height;
int hSelection = hBar.getSelection();
int vSelection = vBar.getSelection();
if (hSelection >= hPage) {
if (hPage <= 0) {
hSelection = 0;
}
origin.x = -hSelection;
}
if (vSelection >= vPage) {
if (vPage <= 0) {
vSelection = 0;
}
origin.y = -vSelection;
}

// Reposition label and text
label.setBounds(origin.x + labelBounds.x, origin.y
+ labelBounds.y, labelBounds.width, labelBounds.height);
text.setBounds(origin.x + textBounds.x,
origin.y + textBounds.y, textBounds.width,
textBounds.height);
text2.setBounds(origin.x + textBounds.x,
origin.y + textBounds.y + 100, textBounds.width,
textBounds.height);


//canvas.redraw(); causes flash
// only redaw the border parts
composite.redraw(0, 0, client.width, 1, false);
composite.redraw(0, 0, 1, client.height, false);
composite.redraw(0, client.height - 1, client.width, 1, false);
composite.redraw(client.width - 1, 0, 1, client.height, false);
}
});
composite.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
Color white = event.display.getSystemColor(SWT.COLOR_WHITE);
Color black = event.display.getSystemColor(SWT.COLOR_BLACK);
event.gc.setBackground(white);
event.gc.fillRectangle(origin.x, origin.y, FORM_SIZE.width - 1,
FORM_SIZE.height - 1);
event.gc.setForeground(black);
event.gc.drawRectangle(origin.x, origin.y, FORM_SIZE.width - 1,
FORM_SIZE.height - 1);
event.gc.setBackground(composite.getBackground());
// avoid flash by not drawing background behind white area
// create composite with SWT.NO_BACKGROUND and only draw background
// outside of FORM_SIZE
Rectangle area = composite.getClientArea();
if (area.width > FORM_SIZE.width) {
event.gc.fillRectangle(FORM_SIZE.width, 0, area.width -
FORM_SIZE.width, area.height);
}
if (area.height > FORM_SIZE.height) {
event.gc.fillRectangle(0, FORM_SIZE.height, area.width, area.height -
FORM_SIZE.height);
}
}
});

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

"Christian Hauser" <christian.hauser@dvbern.ch> wrote in message
news:cip249$r8d$1@eclipse.org...
> Hi all
>
> Veronika has posted some code to the platform-swt-dev mailing list and
> provided code that shows how to scroll a canvas. She added a key listener
> to the canvas so that the canvas accepts focus.
>
> However, I have still problems with getting the focus of the canvas, if I
> have (see code below) a Button *AND* a Canvas. If I only have the canvas,
> it is no problem to scroll (using the mouse wheel).
>
> What I want is to be able in the code below to scroll up and down within
> the canvas.
>
> This works well if I select give focus to the text field or if I comment
> out the following line in the main method:
> //new Button(shell, SWT.PUSH).setText("Hello");
> This makes the canvas the only composite within the shell.
>
> I'd be very happy if anyone could help me on that. I tried to keep the
> example of the form (code below) as small as possible, however, I still
> wanted to provide working code.
>
> Thanks in advance for any help on that.
> Christian
>
>
> /* FormCanvas.java */
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.*;
> import org.eclipse.swt.graphics.*;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.*;
>
> public class FormCanvas extends Canvas {
> private static final Rectangle FORM_SIZE = new Rectangle(0, 0, 500,
> 900);
> private static final Point origin = new Point(0, 0);
>
> private ScrollBar hBar;
> private ScrollBar vBar;
>
> private Label label;
> private Rectangle labelBounds = new Rectangle(10, 53, 60, 20);
> private Text text;
> private Rectangle textBounds = new Rectangle(70, 50, 100, 20);
>
> public FormCanvas(Composite parent) {
> super(parent, SWT.H_SCROLL | SWT.V_SCROLL);
>
> setBounds(FORM_SIZE);
> addKeyListener(new KeyAdapter() {
> }); // makes canvas accept focus (according to Veronika)
> ScrollBarListener scrollBarListener = new ScrollBarListener(this);
> hBar = getHorizontalBar();
> hBar.addSelectionListener(scrollBarListener);
> vBar = getVerticalBar();
> vBar.addSelectionListener(scrollBarListener);
> addListener(SWT.Resize, new FormResizeListener(this));
> addPaintListener(new FormPaintListener());
>
> // Add label
> label = new Label(this, SWT.NONE);
> label.setText("Your name:");
> label.setBounds(labelBounds);
> Color white = new Color(parent.getDisplay(), 0xEE, 0xEE, 0xEE);
> label.setBackground(white);
> white.dispose();
>
> // Add text field
> text = new Text(this, SWT.BORDER | SWT.SINGLE);
> text.setBounds(textBounds);
> }
>
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
>
> new Button(shell, SWT.PUSH).setText("Hello");
>
> Canvas canvas = new FormCanvas(shell);
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
>
> /**
> * Paint listener for the canvas.
> */
> private class FormPaintListener implements PaintListener {
> public void paintControl(PaintEvent event) {
> Color white = new Color(event.display, 0xEE, 0xEE, 0xEE);
> Color black = new Color(event.display, 0x00, 0x00, 0x00);
> event.gc.setBackground(white);
> event.gc.fillRectangle(origin.x, origin.y, FORM_SIZE.width - 1,
> FORM_SIZE.height - 1);
> event.gc.setForeground(black);
> event.gc.drawRectangle(origin.x, origin.y, FORM_SIZE.width - 1,
> FORM_SIZE.height - 1);
> white.dispose();
> black.dispose();
> }
> }
>
> /**
> * Listener for horizontal and vertical scroll bar.
> */
> private class ScrollBarListener extends SelectionAdapter {
> private Canvas canvas;
>
> ScrollBarListener(Canvas canvas) {
> this.canvas = canvas;
> }
>
> public void widgetSelected(SelectionEvent event) {
> int hSelection = hBar.getSelection();
> int vSelection = vBar.getSelection();
> int destX = -hSelection - origin.x;
> int destY = -vSelection - origin.y;
> Rectangle rect = FORM_SIZE;
> canvas.scroll(destX, destY, 0, 0, rect.width, rect.height, false);
> origin.x = -hSelection;
> origin.y = -vSelection;
>
> // Reposition label and text
> label.setBounds(origin.x + labelBounds.x, origin.y + labelBounds.y,
> labelBounds.width, labelBounds.height);
> text.setBounds(origin.x + textBounds.x, origin.y + textBounds.y,
> textBounds.width, textBounds.height);
> }
> }
>
> /**
> * Listener that handles the form resize events.
> */
> private class FormResizeListener implements Listener {
> private Canvas canvas;
>
> FormResizeListener(Canvas canvas) {
> this.canvas = canvas;
> }
>
> public void handleEvent(Event event) {
> Rectangle client = canvas.getClientArea();
> hBar.setMaximum(FORM_SIZE.width);
> vBar.setMaximum(FORM_SIZE.height);
> hBar.setThumb(Math.min(FORM_SIZE.width, client.width));
> vBar.setThumb(Math.min(FORM_SIZE.height, client.height));
> int hPage = FORM_SIZE.width - client.width;
> int vPage = FORM_SIZE.height - client.height;
> int hSelection = hBar.getSelection();
> int vSelection = vBar.getSelection();
> if (hSelection >= hPage) {
> if (hPage <= 0) {
> hSelection = 0;
> }
> origin.x = -hSelection;
> }
> if (vSelection >= vPage) {
> if (vPage <= 0) {
> vSelection = 0;
> }
> origin.y = -vSelection;
> }
>
> // Reposition label and text
> label.setBounds(origin.x + labelBounds.x, origin.y + labelBounds.y,
> labelBounds.width, labelBounds.height);
> text.setBounds(origin.x + textBounds.x, origin.y + textBounds.y,
> textBounds.width, textBounds.height);
>
> canvas.redraw();
> }
> }
> }
Re: How to scroll a Canvas? [message #443215 is a reply to message #443209] Tue, 21 September 2004 20:05 Go to previous messageGo to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
Thanks a lot for your help and useful tips, Veronika.

Do you know coincidentally if there is a proper way to scroll more than
just a few pixel using the mouse wheel? On a large form one has to
scroll too long to get to the end of the form, whereas within the
Eclipse editor it scrolls 3 lines at a time.

Thanks again,
Christian



Veronika Irvine wrote:

> If your canvas has children, then it is an enitrely different story.
> Actually looking at your code, I recommend that you use a composite instead.
>
> You need to detect the mouse down and set focus if it is appropriate. I
> have modified your example to work below. Note that without the mouse down
> listener, the code below still works if the focus is given to the text
> widget. Also note that setting focus when a mouse down occurs in the blank
> background of a composite is not expected platform behaviour.
>
> I also modified your code to get rid of some unneccesdsary work and to stop
> the flashing.
>
>
> private static final Rectangle FORM_SIZE = new Rectangle(0, 0, 500, 900);
>
> private static final Point origin = new Point(0, 0);
>
> private static final Rectangle labelBounds = new Rectangle(10, 53, 60, 20);
>
> private static final Rectangle textBounds = new Rectangle(70, 50, 100, 20);
>
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> new Button(shell, SWT.PUSH).setText("Hello");
> final Composite composite = new Composite(shell, SWT.H_SCROLL |
> SWT.V_SCROLL | SWT.NO_BACKGROUND);
> //canvas.setBounds(FORM_SIZE); // useless since you have a layout on shell
> final ScrollBar hBar = composite.getHorizontalBar();
> final ScrollBar vBar = composite.getVerticalBar();
> // canvas.addKeyListener(new KeyAdapter() {
> // makes canvas accept focus (according to Veronika)
> // }); // not required because you have children
> SelectionListener scrollBarListener = new SelectionAdapter() {
> public void widgetSelected(SelectionEvent event) {
> int hSelection = hBar.getSelection();
> int vSelection = vBar.getSelection();
> // following lines not needed because composite already does this part
> // int destX = -hSelection - origin.x;
> // int destY = -vSelection - origin.y;
> // Rectangle rect = FORM_SIZE;
> // canvas.scroll(destX, destY, 0, 0, rect.width, rect.height, false);
> origin.x = -hSelection;
> origin.y = -vSelection;
> composite.notifyListeners(SWT.Resize, new Event());
> }
> };
> hBar.addSelectionListener(scrollBarListener);
> vBar.addSelectionListener(scrollBarListener);
>
> // this behaviour is not normal platform behaviour
> // but it enable steh mousewheel to scroll when
> // the user clicks anywhere in the composite
> composite.addListener(SWT.MouseDown, new Listener() {
> public void handleEvent(Event event) {
> Control focus = event.display.getFocusControl();
> while (focus != null) {
> if (focus == composite) return;
> focus = focus.getParent();
> }
> composite.setFocus();
> }
> });
>
> // Add label
> final Label label = new Label(composite, SWT.NONE);
> label.setText("Your name:");
> label.setBounds(labelBounds);
> Color white = display.getSystemColor(SWT.COLOR_WHITE);
> label.setBackground(white);
>
> // Add text field
> final Text text = new Text(composite, SWT.BORDER | SWT.SINGLE);
>
> final Text text2 = new Text(composite, SWT.BORDER | SWT.SINGLE);
>
> // bounds setting for text and label not required because done by resize
> listener
> composite.addListener(SWT.Resize, new Listener() {
> public void handleEvent(Event event) {
> Rectangle client = composite.getClientArea();
> hBar.setMaximum(FORM_SIZE.width);
> vBar.setMaximum(FORM_SIZE.height);
> hBar.setThumb(Math.min(FORM_SIZE.width, client.width));
> vBar.setThumb(Math.min(FORM_SIZE.height, client.height));
> int hPage = FORM_SIZE.width - client.width;
> int vPage = FORM_SIZE.height - client.height;
> int hSelection = hBar.getSelection();
> int vSelection = vBar.getSelection();
> if (hSelection >= hPage) {
> if (hPage <= 0) {
> hSelection = 0;
> }
> origin.x = -hSelection;
> }
> if (vSelection >= vPage) {
> if (vPage <= 0) {
> vSelection = 0;
> }
> origin.y = -vSelection;
> }
>
> // Reposition label and text
> label.setBounds(origin.x + labelBounds.x, origin.y
> + labelBounds.y, labelBounds.width, labelBounds.height);
> text.setBounds(origin.x + textBounds.x,
> origin.y + textBounds.y, textBounds.width,
> textBounds.height);
> text2.setBounds(origin.x + textBounds.x,
> origin.y + textBounds.y + 100, textBounds.width,
> textBounds.height);
>
>
> //canvas.redraw(); causes flash
> // only redaw the border parts
> composite.redraw(0, 0, client.width, 1, false);
> composite.redraw(0, 0, 1, client.height, false);
> composite.redraw(0, client.height - 1, client.width, 1, false);
> composite.redraw(client.width - 1, 0, 1, client.height, false);
> }
> });
> composite.addPaintListener(new PaintListener() {
> public void paintControl(PaintEvent event) {
> Color white = event.display.getSystemColor(SWT.COLOR_WHITE);
> Color black = event.display.getSystemColor(SWT.COLOR_BLACK);
> event.gc.setBackground(white);
> event.gc.fillRectangle(origin.x, origin.y, FORM_SIZE.width - 1,
> FORM_SIZE.height - 1);
> event.gc.setForeground(black);
> event.gc.drawRectangle(origin.x, origin.y, FORM_SIZE.width - 1,
> FORM_SIZE.height - 1);
> event.gc.setBackground(composite.getBackground());
> // avoid flash by not drawing background behind white area
> // create composite with SWT.NO_BACKGROUND and only draw background
> // outside of FORM_SIZE
> Rectangle area = composite.getClientArea();
> if (area.width > FORM_SIZE.width) {
> event.gc.fillRectangle(FORM_SIZE.width, 0, area.width -
> FORM_SIZE.width, area.height);
> }
> if (area.height > FORM_SIZE.height) {
> event.gc.fillRectangle(0, FORM_SIZE.height, area.width, area.height -
> FORM_SIZE.height);
> }
> }
> });
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
Re: How to scroll a Canvas? [message #443216 is a reply to message #443215] Tue, 21 September 2004 20:50 Go to previous messageGo to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
> Do you know coincidentally if there is a proper way to scroll more than
> just a few pixel using the mouse wheel? On a large form one has to
> scroll too long to get to the end of the form, whereas within the
> Eclipse editor it scrolls 3 lines at a time.

What I did to not just scroll by 3 pixel (with my mouse I get 3 events
for one "click" on the wheel) is the following:

SelectionListener scrollBarListener = new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
int hSelection = hBar.getSelection();
int vSelection = vBar.getSelection();
if (event.detail == SWT.ARROW_UP)
vSelection *= 4;
else if (event.detail == SWT.ARROW_DOWN)
vSelection *= 4;
origin.x = -hSelection;
origin.y = -vSelection;
notifyListeners(SWT.Resize, new Event());
}
};

This way my mouse can scroll 4 times faster using the wheel on it.
However, I still don't know whether this is a good approach.

Any comments appreciated.

Christian
Re: How to scroll a Canvas? [message #443217 is a reply to message #443216] Tue, 21 September 2004 21:42 Go to previous message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
Sorry for all the trouble. I just found it out by myself:

vBar = getVerticalBar();
// Speed up scrolling when using a wheel mouse
vBar.setIncrement(10); // used to be 1

Now my wheel mouse scrolls faster.

Sorry for bothering you,
Christian
Previous Topic:ComboBoxPropertyDescriptor
Next Topic:OS X, Arrow Controls
Goto Forum:
  


Current Time: Thu Apr 25 12:34:54 GMT 2024

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

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

Back to the top