Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » adding event to a line
adding event to a line [message #484974] Thu, 10 September 2009 04:44 Go to next message
caesar  is currently offline caesar Friend
Messages: 6
Registered: July 2009
Junior Member
I am trying to adding an event to a line so that I can drag a line on composite.

Here is my code.

	    final Composite composite = new Composite(scomposite, SWT.NONE);
	    Color backgroundColor = new Color(composite.getDisplay(), 246, 160, 11);
	    scomposite.setContent(composite);
	    composite.setLayout(null);
	    composite.setSize(editorWidth, editorHeight);
	    composite.setBackground(backgroundColor);
	    composite.addPaintListener(new PaintListener() {
		public void paintControl(PaintEvent e) {
		    e.gc.drawLine(Constants.LEFT_XCOORDINATE, Constants.LEFT_YCOORDINATE, Constants.HORIZONTAL_WIDTH, 20);
		    e.gc.drawLine(20, 20, 20, 
		}
	    });


Is there a way to make a draggable line? I want a user to drag a line on a composite. It would be a great help if anybody tells me an alternative way to do it.

Please help.

Thanks.
Re: adding event to a line [message #486163 is a reply to message #484974] Wed, 16 September 2009 15:03 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
If it's a horizontal or vertical line then you could use a Tracker instead.
For an example of this see
http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/org. eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet23. java
(change the Rectangle's width or height from 100 to 2).

For lines that aren't horizontal or vertical you'll need a custom approach.
I've pasted an example of this below, which demonstrates a vertical line (I
chose this case because it's straight-forward). You can substitute a
Composite for Canvas, it doesn't matter here.

public class Main4 {
static Rectangle line = new Rectangle(100,100,1,100);
static int pointerOffset = 0;
static boolean isDragging = false;
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setBounds(10,10,400,400);
shell.setLayout(new FillLayout());
final Canvas canvas = new Canvas(shell, SWT.NONE);
canvas.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event event) {
event.gc.drawLine(line.x, line.y, line.x, line.y +
line.height);
}
});
canvas.addListener(SWT.DragDetect, new Listener() {
public void handleEvent(Event event) {
if (Math.abs(event.x - line.x) < 2 && line.y <= event.y &&
event.y <= (line.y + line.height)) {
isDragging = true;
pointerOffset = event.y - line.y;
}
}
});
canvas.addListener(SWT.MouseUp, new Listener() {
public void handleEvent(Event event) {
isDragging = false;
}
});
canvas.addListener(SWT.MouseMove, new Listener() {
public void handleEvent(Event event) {
if (!isDragging) return;
canvas.redraw(line.x, line.y, line.width, line.height + 1,
false); // erase the old line
line.x = event.x;
line.y = event.y - pointerOffset;
canvas.redraw(line.x, line.y, line.width, line.height + 1,
false); // draw the new line
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}

Grant


"caesar" <caesarkim@hotmail.com> wrote in message
news:h8a06s$m6p$1@build.eclipse.org...
> I am trying to adding an event to a line so that I can drag a line on
composite.
>
> Here is my code.
>
>
> final Composite composite = new Composite(scomposite, SWT.NONE);
> Color backgroundColor = new Color(composite.getDisplay(), 246, 160,
11);
> scomposite.setContent(composite);
> composite.setLayout(null);
> composite.setSize(editorWidth, editorHeight);
> composite.setBackground(backgroundColor);
> composite.addPaintListener(new PaintListener() {
> public void paintControl(PaintEvent e) {
> e.gc.drawLine(Constants.LEFT_XCOORDINATE, Constants.LEFT_YCOORDINATE,
Constants.HORIZONTAL_WIDTH, 20);
> e.gc.drawLine(20, 20, 20,
> }
> });
>
>
> Is there a way to make a draggable line? I want a user to drag a line on
a composite. It would be a great help if anybody tells me an alternative way
to do it.
>
> Please help.
>
> Thanks.
Previous Topic:limiting tree control height with formlayout
Next Topic:How to capture and produce IME composition Event?
Goto Forum:
  


Current Time: Fri Apr 26 02:51:21 GMT 2024

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

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

Back to the top