当我启动我的 Java swing 应用程序时,我将一个 XML 文件读入 DefaultTreeModel,使用该模型创建一个新的 JTree,将 jtree 添加到 JScrollPane,这样就可以了。一切正常:
// Program initialization:
jarFilesDB = new JarFilesDB ();
jarFilesDB.load(JarFilesDB.JAR_DB_FILENAME);
jtree = new JTree(jarFilesDB.getRootNode ());
JScrollPane scrollPane = new JScrollPane (jtree);
contentPane.add(scrollPane, BorderLayout.CENTER);
稍后,我可能想创建一个新的XML 文件,并完全重建我的模型。当我这样做时,我无法让 jtree 重新显示新模型。要么我得到一个空白窗格,要么旧模型仍然显示:
// Completely regenerate XML file and Model
jarFilesDB.mkXmlFile (sPath); // Generate new XML file
jarFilesDB.load(JarFilesDB.JAR_DB_FILENAME); // Generate new Model
根据 Andrew Thompson 的要求,我写了一个“简短(尽可能)、独立、正确(不是!否则我不会问这个问题)示例):
package com.rgb;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.tree.*;
public class FrmMain extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrmMain frame = new FrmMain();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FrmMain() {
setTitle("HelloJTree3");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnNewButton = new JButton("Click Me");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
doClick ();
}
});
contentPane.add(btnNewButton, BorderLayout.NORTH);
jarFilesDB = new JarFilesDB ();
jarFilesDB.load(isAlpha);
isAlpha = !isAlpha;
jtree = new JTree(jarFilesDB.getRootNode ());
JScrollPane scrollPane = new JScrollPane (jtree);
contentPane.add(scrollPane, BorderLayout.CENTER);
}
private void doClick () {
jarFilesDB.load(isAlpha);
isAlpha = !isAlpha;
}
private JarFilesDB jarFilesDB;
private JTree jtree;
private boolean isAlpha = false;
}
class JarFilesDB extends DefaultTreeModel {
public JarFilesDB () {
super (null);
}
public void load (boolean isAlpha) {
if (rootNode == null)
rootNode = new DefaultMutableTreeNode("root");
else
rootNode.removeAllChildren ();
String[] values;
if (isAlpha)
values = new String[] {"A", "A1", "A2", "B", "B1", "B2"};
else
values = new String[] {"10", "11", "12", "20", "21", "22"};
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode (values[0]);
DefaultMutableTreeNode leafNode = new DefaultMutableTreeNode (values[1]);
childNode.add(leafNode);
leafNode = new DefaultMutableTreeNode (values[2]);
childNode.add(leafNode);
rootNode.add(childNode);
childNode = new DefaultMutableTreeNode (values[3]);
leafNode = new DefaultMutableTreeNode (values[4]);
childNode.add(leafNode);
leafNode = new DefaultMutableTreeNode (values[5]);
childNode.add(leafNode);
rootNode.add(childNode);
}
public TreeNode getRootNode () {
return rootNode;
}
private DefaultMutableTreeNode rootNode = null;
}