How do I make a rectangle clickable? [message #240818] |
Fri, 18 January 2008 22:21  |
Eclipse User |
|
|
|
I would like to display a rectangle on a canvas and have it respond to
a mouse click. I tried the following and it does not work - the
rectangle does not draw when used as the contents of the Clickable.
Could somone please explain what it means for a Clickable to have
contents. I assumed that the contents of a clickable would the the
figure shown and that the Clickable would simply be a wrapper on top of
the figure handling the mouse actions, but it seems that's not the case.
Canvas canvas = new Canvas(parent, SWT.NONE);
LightweightSystem lws = new LightweightSystem(canvas);
Figure root = new Figure();
RectangleFigure rect = new RectangleFigure();
Clickable click = new Clickable(rect);
click.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.out.println("actionPerformed()");
}
});
rect.setBounds(new Rectangle(10, 10, 100, 100));
root.add(click);
lws.setContents(root);
Bryan
|
|
|
|
|
|
Re: How do I make a rectangle clickable? [message #241073 is a reply to message #240818] |
Tue, 29 January 2008 02:51  |
Eclipse User |
|
|
|
Originally posted by: Gerhard.Hinterndorfer.pedrics.at
Hallo Bryan,
use addMouseListener:
rect.addMouseListener(new MouseListener.Stub(){
public void mousePressed(MouseEvent me){
System.out.println("Mouse pressed on rect:" +"
x-Pos:"+me.x+" y-Pos:"+me.y);
});
Hope that helps.
cheers
Gerhard
Bryan Hunt schrieb:
> I would like to display a rectangle on a canvas and have it respond to a
> mouse click. I tried the following and it does not work - the rectangle
> does not draw when used as the contents of the Clickable. Could somone
> please explain what it means for a Clickable to have contents. I
> assumed that the contents of a clickable would the the figure shown and
> that the Clickable would simply be a wrapper on top of the figure
> handling the mouse actions, but it seems that's not the case.
>
> Canvas canvas = new Canvas(parent, SWT.NONE);
> LightweightSystem lws = new LightweightSystem(canvas);
> Figure root = new Figure();
> RectangleFigure rect = new RectangleFigure();
> Clickable click = new Clickable(rect);
> click.addActionListener(new ActionListener()
> {
> public void actionPerformed(ActionEvent arg0)
> {
> System.out.println("actionPerformed()");
> }
> });
>
> rect.setBounds(new Rectangle(10, 10, 100, 100));
> root.add(click);
> lws.setContents(root);
>
> Bryan
>
|
|
|
Powered by
FUDForum. Page generated in 0.03503 seconds