[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.platform.swt] Context menu position
|
Hi All!
I hope that the SWT newsgroup ist the right place for my problem:
I have a SWT Table in a Eclipse property tab / section. Now I'm trying to
show a context menu on that table.
So I have a composite and added a table on it:
table = this.getWidgetFactory().createTable(parent, SWT.FULL_SELECTION |
SWT.MULTI);
Then I added a menu on the table and some menu items to the menu:
tableMenu = new Menu(table);
MenuItem addItem = new MenuItem(tableMenu, SWT.POP_UP);
...
Additionally I already found an article describing the fact that the mouse
position the MouseEvent returns is relative to the composite the menu
belongs to (so the table in my case). So I found the following "algorithm"
to get the absolute mouse position:
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
super.mouseDown(e);
if (e.button == 3) {
int x = e.x + table.getLocation().x;
int y = e.y + table.getLocation().y;
Composite parent = table.getParent();
while (parent != null) {
x = x + parent.getLocation().x;
y = y + parent.getLocation().y;
parent = parent.getParent();
}
tableMenu.setLocation(x, y);
tableMenu.setVisible(true);
}
}
});
Unfortunately there is still a difference in the x as well as in the y
coordinates. Debugging it shows this difference but no real reason for that
difference. Any ideas out there? Or is there perhaps a better solution and
I'm just on the wrong track?
Best regards,
Markus