gpt4 book ai didi

java - JTree与DefaultTreeModel,加载后如何在视觉上刷新?

转载 作者:行者123 更新时间:2023-12-01 19:11:22 30 4
gpt4 key购买 nike

我一直在这里搜索并找到了这个问题的答案,但似乎无法让它们为我工作。基本上我有一个 GUI,其中包含 JScrollPane 内的 JTree:

private void initComponents() {

scroll = new javax.swing.JScrollPane();
nodo padre = new nodo();
modeloArbol modelo = new modeloArbol(padre);
arbol = new arbolNodos(modelo);

我有以下类(class):

public class arbolNodos extends JTree implements Serializable{

public arbolNodos(TreeModel newModel) {


public class listenerModeloArbol implements TreeModelListener{

//Overriding treeNodes*(TreeModelEvent e)


public class modeloArbol extends DefaultTreeModel implements Serializable{

public modeloArbol(TreeNode root) {
super(root);
}


public class nodo extends DefaultMutableTreeNode implements Serializable{

我还有 2 个按钮,它们使用 XMLEncoder 将 JTree 存储 (botonGuardarArbol) 和检索 (botonCargarArbol)(它们实际上检索树的根节点)到具有相应 actionPerformed 监听器的文件中:

private void botonGuardarArbolActionPerformed(java.awt.event.ActionEvent evt) {                                                  
// TODO add your handling code here:
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("C:\\borrar\\presupuesto")));
encoder.writeObject(arbol.getModel().getRoot());
encoder.close();



private void botonCargarArbolActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
XMLDecoder decoder = new XMLDecoder( new BufferedInputStream(new FileInputStream("C:\\borrar\\presupuesto")));

nodo padre = (nodo) decoder.readObject();

modeloArbol modelo = new modeloArbol(padre);

decoder.close();

modelo.setRoot(padre);

arbolNodos arbolNuevo = null;

if( modelo != null ){
arbolNuevo = new arbolNodos(modelo);
arbol = null;
arbol = arbolNuevo;

((DefaultTreeModel)arbol.getModel()).reload();
}
else
arbolNuevo = new arbolNodos();

问题是,当我检索存储的 JTree 时,我知道它检索得很好,因为我尝试在树的所有节点内打印属性并且它们没问题,但是当我尝试将其放入 Swing 中时却没有。 t 刷新和 View block (即,我无法在加载之前操作 JTree 中的任何内容)。我已经尝试了许多刷新/更新 JTree 的线程,但无法修复它。我怎样才能做到这一点?

谢谢

最佳答案

if( modelo != null )
{
arbolNuevo = new arbolNodos(modelo); //
arbol = null;
arbol = arbolNuevo;
((DefaultTreeModel)arbol.getModel()).reload();
}
else
arbolNuevo = new arbolNodos();

不要继续创建新的 JTree 对象。如果您想更改模型,只需使用:

tree.setModel(...);

另外,为什么要扩展 JTree、DefaultTreeModel、DefaultMutableTreeNode?如果由于某种原因您确实需要扩展这些类,请遵循这些类的标准 Java 命名约定。类以大写字符开头。

关于java - JTree与DefaultTreeModel,加载后如何在视觉上刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8259734/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com