当我选择一个项目然后双击它时,将调用方法mouseDoubleClick()
,但是当我双击之前未选择的项目时,它将无法被调用。为什么?我很难相信这是一个错误,因为这种操作/情况似乎是基本的,必须广泛使用。
要重现它,请执行以下源代码:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class DoubleClickOnATreeCursor {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
// create a a tree with 3 columns and fill with data
final Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
tree.setLayoutData(new GridData(GridData.FILL_BOTH));
tree.setHeaderVisible(true);
TreeColumn column1 = new TreeColumn(tree, SWT.NONE);
TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
TreeColumn column3 = new TreeColumn(tree, SWT.NONE);
for (int i = 0; i < 9; i++) {
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText(new String[] { "root " + i + " 0", "root " + i + " 1", "root " + i + " 2" });
for (int j = 0; j < 9; j++) {
TreeItem item2 = new TreeItem(item, SWT.NONE);
item2.setText(new String[] { "child " + j + " 0", "child " + j + " 1", "child " + j + " 2" });
TreeItem current = item2;
for (int k = 0; k < 5; k++) {
TreeItem item3 = new TreeItem(current, SWT.NONE);
item3.setText(new String[] { "descendent " + k + " 0", "descendent " + k + " 1", "descendent " + k + " 2" });
current = item3;
}
}
}
column1.setWidth(200);
column2.setWidth(100);
column3.setWidth(100);
final TreeCursor cursor = new TreeCursor(tree, SWT.NONE);
cursor.addMouseListener(new MouseListener() {
@Override
public void mouseDoubleClick(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseDoubleClick");
}
@Override
public void mouseDown(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
好的,不确定我是否理解TreeCursor的理念,但在我看来,所选项目是TreeCursor。如果没有选择它,那么谈论TreeCursor是没有意义的。因此,当您单击选择时,只能在TreeCursor上双击。
问题在于我使用TreeCursor树,当用户点击属于TreeCursor的部分时,双击事件不会被触发。但是来自用户&#39;透视它也是树的一部分。
无论在Tree或TreeCursor上点击什么,都可以捕获双击事件吗?