简体   繁体   中英

How to attach data to TreeItem in SWT/Java?

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?

This is simple.

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

treeItem.setData("key", obj);

when getting data :

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM