简体   繁体   English

如何在SWT / Java中将数据附加到TreeItem?

[英]How to attach data to TreeItem in SWT/Java?

I'm starting to use the SWT GUI toolkit in Java. 我开始在Java中使用SWT GUI工具包。 I have a need to attach some data to the TreeItems. 我需要将一些数据附加到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. 我以前使用的每个工具箱都有一个树项,其中包含一个原始指针或一个基础对象引用以提供基本数据包含,但是我在SWT的TreeItem中找不到一个。

How can I attach data to the TreeItem? 如何将数据附加到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. 答案是:使用纯SWT无法做到。
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. 将数据绑定到小部件是基于SWT的更高级的JFace框架(尤其是它的数据绑定工具)的主题。 You need some time to master it, but therefore you gain the power of the Eclipse platform. 您需要一些时间来掌握它,但是因此您可以获得Eclipse平台的功能。 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). (→更改模型对象会立即反映在UI上,反之亦然)。
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() . 在SWT 4.2中,可能在较早的版本中(尽管我没有检查),类TreeItemWidget的子类,并且Widget具有setData(Object o)getData() You can use that to attach arbitrary, application-specific data to TreeItem instances. 您可以使用它将任意特定于应用程序的数据附加到TreeItem实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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