我有一个实现的自定义 JTree 实现convertValueToText
。这个实现依赖于一些全局状态。如果返回的字符串比以前返回的字符串长(实际上我认为以像素为单位触发它更宽),则文本将被截断并用“...”填充。当重绘是由(取消)选择元素或repaint
树上的 a 引起的时,这是正确的。
convertValueToText
以这种方式实施是否有效?(我知道树显示不会自动更新)。
如何摆脱“...”并强制使用当前文本值正确绘制所有元素?
更新2:
显然我违反了 Swings 相当严格的踩踏政策(http://docs.oracle.com/javase/6/docs/api/javax/swing/package-summary.html)。使用以下方法进行更新:
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
for (int row = 0; row < tree.getRowCount(); row++) {
((DefaultTreeModel) tree.getModel())
.nodeChanged((TreeNode) tree.getPathForRow(row)
.getLastPathComponent());
}
}
});
似乎工作正常。
更新:
这是一个最小的例子:
import java.util.Enumeration;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
public class WeirdTreeFrame extends JFrame {
public class WeirdTree extends JTree {
public WeirdTree(TreeNode root) {
super(root);
}
@Override
public String convertValueToText(Object value, boolean selected,
boolean expanded, boolean leaf, int row, boolean hasFocus) {
return super.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
}
}
public class WeirdNode implements TreeNode {
protected WeirdNode parent;
protected WeirdNode children[];
protected int dephth;
protected String name;
protected String names[] = {"Foo", "Bar", "Ohh"};
public long superCrazyNumber;
public WeirdNode(WeirdNode parent, String name) {
this.parent = parent;
this.name = name;
if (parent != null) {
dephth = parent.dephth + 1;
} else {
dephth = 0;
}
if (dephth < 10) {
children = new WeirdNode[3];
for (int i = 0; i < 3; i++) {
children[i] = new WeirdNode(this, name + names[i]);
}
}
}
@Override
public TreeNode getChildAt(int childIndex) {
if (childIndex >= getChildCount()) return null;
return children[childIndex];
}
@Override
public int getChildCount() {
return (children != null) ? children.length : 0;
}
@Override
public TreeNode getParent() {
return parent;
}
@Override
public int getIndex(TreeNode node) {
for (int i = 0; i < children.length; i++) {
if (children[i] == node) return i;
}
throw new RuntimeException();
}
@Override
public boolean getAllowsChildren() {
return true;
}
@Override
public boolean isLeaf() {
return getChildCount() == 0;
}
@Override
public Enumeration children() {
return new Enumeration<TreeNode>() {
int nextIdx = 0;
@Override
public boolean hasMoreElements() {
return nextIdx < getChildCount();
}
@Override
public TreeNode nextElement() {
return getChildAt(nextIdx++);
}
};
}
@Override
public String toString() {
return "[" + dephth + "]" + name + "@" + superCrazyNumber;
}
}
public WeirdNode root;
private WeirdTree tree;
public WeirdTreeFrame() {
root = new WeirdNode(null, "Blubb");
tree = new WeirdTree(root);
add(tree);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}
public void update() {
for (int row = 0; row < tree.getRowCount(); row++) {
((DefaultTreeModel) tree.getModel()).nodeChanged((TreeNode)tree.getPathForRow(row).getLastPathComponent());
}
}
public static void main(String[] args) {
WeirdTreeFrame frame = new WeirdTreeFrame();
Random rnd = new Random(41);
for (long i = 0; ; i++) {
for (WeirdNode node = frame.root; node != null; node = (WeirdNode)node.getChildAt(rnd.nextInt(3))) {
node.superCrazyNumber++;
}
if (i % 1e7 == 0) {
System.out.println("Update: " + i);
frame.update();
}
}
}
}
使用该update()
方法,我尝试触发适当的事件以确保正确更新所有可见节点。正如您所看到的,一些计算在计算过程中并行且周期性地发生,我想更新树标签(而不是结构)。
该update()
方法的问题是某些节点被标记为完全错误,如您在附图中所见(根节点应为“[0] ...”,第n级应为“[n] ..”)
我认为这是一些竞争条件。