1

当我选择一个项目然后双击它时,该方法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 的部分时,不会触发双击事件。但从用户的角度来看,它也是树的一部分。

无论在 Tree 或 TreeCursor 上单击什么,都可以捕获双击事件吗?

4

0 回答 0