How to attach data to TreeItem in SWT/Java?

谁说胖子不能爱 提交于 2019-12-11 02:24:34

问题


I'm starting to use the SWT GUI toolkit in Java. I have a need to attach some data to the TreeItems. Each toolkit I've previously used had a tree item, which contained a raw pointer or a base object reference to provide basic data containment, but I cannot find one in TreeItem in SWT.

How can I attach data to the TreeItem?


回答1:


The answer is: with pure SWT you cant't.
The Standard Widget Toolkit only takes care of the widgets, their hierarchy and the visual representation. Binding data to widgets is topic of the more advanced JFace framework (especially it's databinding facilities) which builds on top of SWT. You need some time to master it, but therefore you gain the power of the Eclipse platform. It enables you to bind a model to view, even in both direction
(→ change to model object are immediately reflected on the UI and vice versa).
Currently, you would have to keep a seperate list of items and need to work with indices.




回答2:


This is simple.

TreeItem treeItem = new TreeItem(tree, SWT.NONE);

treeItem.setData("key", obj);

when getting data :

Object obj = treeItem.getData("key");



回答3:


In SWT 4.2, and possibly in earlier versions (though I didn't check), the class TreeItem is a subclass of Widget, and Widget has setData(Object o) and getData(). You can use that to attach arbitrary, application-specific data to TreeItem instances.



来源:https://stackoverflow.com/questions/11599606/how-to-attach-data-to-treeitem-in-swt-java

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