[英]Java Swing - JTree - setModel graphics bug on more then 2 chained branches
I tried to create my own JTree
Model so I can update the graphics by calling: updateUI();
我试图创建自己的
JTree
模型,以便可以通过调用以下命令来更新图形: updateUI();
I ran into a problem when my JTree
should display a tree with a depth over 2 branches. 当我的
JTree
应该显示一棵深度超过2个分支的树时,我遇到了一个问题。
I use a own class that has extended by java.util.Vector
called: NamedVector
. 我使用由
java.util.Vector
扩展的自有类,称为: NamedVector
。
Here's how the tree should look like: 这是树的外观:
ROOT
Branch
BranchInside
Leaf
Leaf
...
OtherBranch
AndOneMore
...
...
And here's how it looks like: http://imageshack.us/photo/my-images/39/fehlerqq.png/ 外观如下: http : //imageshack.us/photo/my-images/39/fehlerqq.png/
Here's how I set the new TreeModel: 这是我设置新TreeModel的方法:
jTree1.setModel(new javax.swing.tree.TreeModel(){
@Override
public Object getRoot(){
return core.Project.sharedInstance.getTranslationsTree();
}
@Override
public Object getChild(Object parent, int index){
if(parent instanceof String) return null;
return ((core.NamedVector)parent).get(index);
}
@Override
public int getChildCount(Object parent){
if(parent instanceof String) return 0;
return ((core.NamedVector)parent).size();
}
@Override
public boolean isLeaf(Object node){
if(node instanceof String) return true;
return ((core.NamedVector)node).isEmpty();
}
@Override
public int getIndexOfChild(Object parent, Object child){
if(parent instanceof String) return -1;
return ((core.NamedVector)parent).indexOf(child);
}
@Override
public void valueForPathChanged(TreePath path, Object newValue){}
@Override
public void addTreeModelListener(TreeModelListener l){}
@Override
public void removeTreeModelListener(TreeModelListener l){}
});
( core.Project.sharedInstance.getTranslationsTree()
returns the root vector of my tree, when I print it in the console, it gives correct results.) (
core.Project.sharedInstance.getTranslationsTree()
返回我树的根向量,当我在控制台中打印它时,它给出正确的结果。)
And here is my NamedVector
class: 这是我的
NamedVector
类:
public class NamedVector extends java.util.Vector{
public String name;
public NamedVector(String name){
this.name = name;
}
@Override
public String toString(){
return name;
}
public static void dirToVector(java.io.File sourceLocation, NamedVector target){
if(sourceLocation.isDirectory()){
String[] children = sourceLocation.list();
for(int i=0; i<children.length; i++){
NamedVector vector = new NamedVector(children[i]);
target.add(vector);
dirToVector(new java.io.File(sourceLocation, children[i]),
vector);
}
}
}
public void print(){
print("");
}
private void print(String pre){
System.out.println(pre+name);
pre+=" ";
for(int i=0;i<this.size();i++){
if(get(i) instanceof NamedVector){
((NamedVector)get(i)).print(pre);
}else{
System.out.println(pre+get(i).toString());
}
}
}}
This only happens when I use my own model on the tree, if I build it with DefaultMutableTreeNode
it displays everything correct, but I would like to use a custom model. 仅当我在树上使用自己的模型时才会发生这种情况,如果我使用
DefaultMutableTreeNode
构建,则它会显示所有正确的内容,但是我想使用自定义模型。
FileTreeModel
is an example that implements TreeModel
for a typical hierarchical file system. FileTreeModel
是为典型的分层文件系统实现TreeModel
的示例。 There's an illustration in the article and here . 文章中和此处都有一个插图。 It maybe useful to compare your implementation.
比较您的实现可能很有用。
My best guess, based on your comment 我的最佳猜测,根据您的评论
But for DefaultTreeModel i have to add my nodes manually when i change my vector, i would like only changing my vector and calling the updateUI() method so my tree is builded by his own
但是对于DefaultTreeModel,我必须在更改矢量时手动添加节点,我只想更改矢量并调用updateUI()方法,以便我的树由他自己构建
is that your TreeModel
does not fire the correct events. 是您的
TreeModel
没有触发正确的事件。 So when a change occurs in your vector, the JTree
is not informed. 因此,当向量发生更改时,不会通知
JTree
。 Afterwards, when it repaints/expands a node/... and asks the TreeModel
for information, this information will be out of sync with the info the JTree
received through the events, typically resulting in JTree
s as shown in your pictures. 之后,当它重新绘制/扩展节点/ ...并询问
TreeModel
信息时,此信息将与事件中收到的JTree
的信息不同步,通常会导致JTree
,如您的图片所示。
Certainly if your model has empty implementations for the addTreeModelListener
method 当然,如果您的模型对
addTreeModelListener
方法有空的实现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.