SWT: prevent Tree from expanding by doubleclick?

冷暖自知 提交于 2019-12-08 08:24:31

问题


I have a problem with SWT Tree. My situation is like this: I have a SWT Tree, which contains many TreeItems (Log entries), which contain TreeItems too. Those log entries have really long messages, which could not be shown in the TreeColumns at all. So my idea was: adding a Listener to the tree, which opens a new Dialog by DoubleClick, which shows the entries' details. So far so good. If I do a double click on a item, it works. BUT: If I do a double click on a parent Item, it will expand (and thats good), but my double click Listener is active then as well and the Dialog will open. That's not, what I want.

So, there are two solutions to the problem: 1) prevent the Tree from expanding/collapsing by double click automatically and implement the method by myself or 2) recognize, that the item was expanded and the event has to be aborted.

I do not really know how to do 1 or 2. Do u guys know that?

Thanks in advance.


回答1:


If you are using TreeViewer, you could make use of IOpenListener

treeViewer.addOpenListener(new IOpenListener() {

      @Override
      public void open(OpenEvent event) {
}
}



回答2:


I find the above answers to be rather bogus. The following worked for me:

treeViewer.getControl().addListener(SWT.MeasureItem, new Listener(){
    @Override
    public void handleEvent(Event event) {
    }});

I know, it does not make any sense; for an explanation of the real cause of the problem, and why the above is a fix, see this discussion in the Eclipse Community Forums: Disabling Treeviewer doubleclick expand/collapse.




回答3:


There is another solution which works much better.

The problem with the solution from 'sambi reddy' was, that the tree was prevented from expanding by doubleclick, but it was prevented from expanding by clickling on the left handside cross as well.

My solution (that works well), was easy: I added a TreeListener, which listens to expanding/collapsing the tree and removed the expanding/collpasing implementation from the MouseDoubleClick-Listener.

No JFace TreeViewer - it works fine.



来源:https://stackoverflow.com/questions/15006888/swt-prevent-tree-from-expanding-by-doubleclick

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!