问题描述
我必须在gwt上实现一棵树。我正在实现一个扩展Widget的对象,将它设置在TreeItem上。
问题是,如果我在TreeItem上设置此对象(只有Strings和Integers字段)我再也看不到树了。
问题在哪里?我看不到它。
私有树frontEndTree; $ c
$ b private void buildTree(HashMap< Long,Categoria> categoriaMap){
for(Categoria categoria:categoriaMap.values()){
if(categoria.getLevel ()!= 1)继续;
TreeItem firstLevel = //this.frontEndTree.addTextItem(categoria.getDescription());
this.frontEndTree.addTextItem(categoria.getDescription());
ItemTree itemTree = new ItemTree(categoria);
firstLevel.setWidget((Widget)itemTree);
this.visitaAlbero(firstLevel,categoria.getDescendants());
}
}
private void visitaAlbero(TreeItem parentItem,ArrayList< Categoria> categorie){
if(categorie == null)return;
if(categorie.isEmpty())return;
TreeItem node = null; (categoria item:categorie){
if(node == null){
node = parentItem.addTextItem(item.getDescription());
node.setWidget(new ItemTree(item));
visitaAlbero(node,item.getDescendants());
继续;
}
node.addTextItem(item.getDescription());
node.setWidget(new ItemTree(item));
visitaAlbero(node,item.getDescendants());
$ b $ **
*内部类别小部件主要
* del treeitem
*
* @author GiuseppePedullà
*
* /
公共类ItemTree扩展Widget实现Serializable,IsWidget {
/ **
*
* /
private static final long serialVersionUID = 5347332077472260376L;
私人长ID;
私人字符串描述;
私人整数等级;
public ItemTree(Categoria categoria){
this.id = categoria.getId();
this.description = categoria.getDescription();
this.level = categoria.getLevel();
}
public String getDescription(){
String valueID =;
if(id< 10){
valueID =00+ id;
} else if(id< 100){
valueID =0+ id;
} else {
valueID =+ id;
}
返回valueID + - + description;
}
// GETTERS
public Long getId(){
return id;
}
public Integer getLevel(){
return level;
}
@Override
public String toString(){
return description;
}
}
问题出现在 ItemTree 类中。它扩展了Widget,但没有为该Widget设置任何元素。至少它缺少 asWidget 方法。它可以是这样的例子:
@Override
public Widget asWidget(){
return new标签(getDescription());
}
你可以像这样使用它:
firstLevel.setWidget(itemTree.asWidget());
更简单的方法是将 Composite 而不是 Widget 。您只需要在构造函数中调用 initWidget 方法即可:
public class ItemTree extends Composite实现Serializable,IsWidget {
...
public ItemTree(Categoria categoria){
this.id = categoria.getId();
this.description = categoria.getDescription();
this.level = categoria.getLevel();
initWidget(new Label(getDescription())); //< - !!!
}
...
}
您可以使用任何 Widget 来初始化您想要的 - Label是最简单的工作示例 - 我已成功对其进行测试。 p>
I have to implements a tree on gwt. I'm implementing an object that extends Widget to set it on a TreeItem.
The problem is that if I set on a TreeItem this object (that have only Strings and Integers fields) I can't see anymore the tree.
Where is the problem? I can't see it.
private Tree frontEndTree; private void buildTree(HashMap<Long, Categoria> categoriaMap) { for(Categoria categoria : categoriaMap.values()) { if(categoria.getLevel() != 1) continue; TreeItem firstLevel = //this.frontEndTree.addTextItem(categoria.getDescription()); this.frontEndTree.addTextItem(categoria.getDescription()); ItemTree itemTree = new ItemTree(categoria); firstLevel.setWidget((Widget)itemTree); this.visitaAlbero(firstLevel, categoria.getDescendants()); } } private void visitaAlbero(TreeItem parentItem, ArrayList<Categoria> categorie) { if(categorie == null) return; if(categorie.isEmpty()) return; TreeItem node = null; for(Categoria item : categorie) { if(node == null) { node = parentItem.addTextItem(item.getDescription()); node.setWidget(new ItemTree(item)); visitaAlbero(node, item.getDescendants()); continue; } node.addTextItem(item.getDescription()); node.setWidget(new ItemTree(item)); visitaAlbero(node, item.getDescendants()); } } /** * Inner class che gestisce il widget principale * del treeitem * * @author Giuseppe Pedullà * */ public class ItemTree extends Widget implements Serializable, IsWidget { /** * */ private static final long serialVersionUID = 5347332077472260376L; private Long id; private String description; private Integer level; public ItemTree(Categoria categoria) { this.id = categoria.getId(); this.description = categoria.getDescription(); this.level = categoria.getLevel(); } public String getDescription() { String valueID = ""; if(id < 10) { valueID = "00" + id; } else if(id < 100) { valueID = "0" + id; } else { valueID = "" + id; } return valueID + " - " + description; } //GETTERS public Long getId() { return id; } public Integer getLevel() { return level; } @Override public String toString() { return description; } }
The problem is in your ItemTree class. It extends Widget but does not set any element for that widget. At least it lacks asWidget method. It could be for example like this:
@Override public Widget asWidget() { return new Label(getDescription()); }
And you could use it like this:
firstLevel.setWidget(itemTree.asWidget());
The easier method is to extend Composite instead of Widget. You would only need to call initWidget method in the constructor:
public class ItemTree extends Composite implements Serializable, IsWidget { ... public ItemTree(Categoria categoria) { this.id = categoria.getId(); this.description = categoria.getDescription(); this.level = categoria.getLevel(); initWidget(new Label(getDescription())); // <- !!! } ... }
You can initWidget with whatever Widget you want - Label is the simplest working example - I have successfully tested it.
这篇关于将小部件设置为gwt上的TreeItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!