Question

I have a treeViewer in which I want to execute some code(doSomething() method) only when mouse up is pressed, not on mouse down. The following piece of code executes it even when mouse down is triggered as selectionlistener is already triggered. What should I do to get the exact behaviour ?

    treeViewer.getTree().addMouseListener(new MouseListener() {

        @Override
        public void mouseUp(MouseEvent e) {
            doSomething()
        }

        @Override
        public void mouseDown(MouseEvent e) {
            // nothing happens
        }

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            // nothing happens
        }
    });

    treeViewer.addPostSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            doSomething()
        }
    });
Was it helpful?

Solution

try this,assuming viewer is your TreeViewer,but it does not work when the mouse was actioned on another control.

viewer.getTree().addMouseListener(new MouseListener() {
        @Override
        public void mouseDoubleClick(MouseEvent e) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mouseDown(MouseEvent e) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mouseUp(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("Mouse UP");

        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top