我正在制作一个JTree
经常更新的模拟器。树中的节点都是JProgressBars
.
我正在打电话tree.treeModel.insertNodeInto(,,)
并treeModel.reload()
更新树。问题是它会立即更新自己而无需等待repaint()
电话。这会导致树闪烁。我尝试设置setDoubleBuffered(true)
节点、树和它所在的容器,但没有任何变化。
这是我的代码:(类File extends DefaultMutableTreeNode
)
public class FileTree extends JTree {
DefaultTreeModel myTreeModel;
FileTree(File root) {
super(root);
myTreeModel = (DefaultTreeModel) this.getModel();
this.treeModel = myTreeModel;
this.setCellRenderer(new ProgressBarCellRenderer());
this.setEditable(true);
}
public void update(File file, File parent) {
myTreeModel.insertNodeInto(file, parent, parent.getChildCount());
myTreeModel.reload();
}
class ProgressBarCellRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
File file = (File) value;
JProgressBar progressBar;
progressBar = new JProgressBar(0, file.length);
progressBar.setValue(file.completed);
progressBar.setPreferredSize(new Dimension((int)(Math.log(file.size)*50),10));
return progressBar;
}
}
}