我開發了一個應用程序來搜索JTree
中的節點。每當「獲取原始樹」按鈕被點擊時,clonedRoot
應填充原始數據。所有的處理都在clonedRoot
上執行。Jtree未更新
除了更新的clonedRoot
未在面板上呈現外,一切工作正常(推導控制檯輸出)。
clonedRoot填充了原始數據
if(ae.getSource()==getOriginalTree)
{
System.out.println("Get original tree");
System.out.println("Nodes present under cloned Root before deep copying");
DisplayNodes(clonedRoot);
getDeepCopy();
System.out.println("Nodes present under cloned Root after deep copying");
DisplayNodes(clonedRoot);
DefaultTreeModel newModel = new DefaultTreeModel(clonedRoot);
clonedTree.setModel(newModel);
for (int i = 0; i < clonedTree.getRowCount(); i++)
{
clonedTree.expandRow(i);
}
System.out.println("Updated tree");
}
後執行搜索操作
Console Output on clicking "Get Original Tree"-
Get original tree
Nodes present under cloned Root before deep copying
A
A1
Cloning done
Nodes present under cloned Root after deep copying
A
A1
A2
A3
B
B1
B2
B3
C
C1
C2
C3
D
D1
D2
D3
E
E1
E2
E3
Updated tree
Initialization Code in ctor
{
root = new DefaultMutableTreeNode("Root");
tree = new JTree(root);
setLAF();
populateTree();
copyBuilder = new DeepCopyJTreeAlt(tree);
getDeepCopy();
System.out.println("Original Tree");
displayTree(tree);
System.out.println("Cloned Tree");
displayTree(clonedTree);
label = new JLabel("Serach Node");
field = new JTextField();
for (int i = 0; i < clonedTree.getRowCount(); i++)
{
clonedTree.expandRow(i);
}
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
pane = new JScrollPane(clonedTree);
centralPanel = new JPanel();
centralPanel.setLayout(new BorderLayout());
submit = new JButton("Search");
submit.addActionListener(new SearchActionListener());
getOriginalTree = new JButton("Get Original Tree");
getOriginalTree.addActionListener(new SearchActionListener());
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(0, 4));
buttonPanel.add(label);
buttonPanel.add(field);
buttonPanel.add(submit);
buttonPanel.add(getOriginalTree);
centralPanel.add(pane, BorderLayout.CENTER);
centralPanel.add(buttonPanel, BorderLayout.SOUTH);
frame = new JFrame();
frame.add(centralPanel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
frame.setVisible(true);
}
private void populateTree()
{
A = addAFile("A", root);
A1 = addAFile("A1", A);
A2 = addAFile("A2", A);
A3 = addAFile("A3", A);
B = addAFile("B", root);
B1 = addAFile("B1", B);
B2 = addAFile("B2", B);
B3 = addAFile("B3", B);
C = addAFile("C", root);
C1 = addAFile("C1", C);
C2 = addAFile("C2", C);
C3 = addAFile("C3", C);
D = addAFile("D", root);
D1 = addAFile("D1", D);
D2 = addAFile("D2", D);
D3 = addAFile("D3", D);
E = addAFile("E", root);
E1 = addAFile("E1", E);
E2 = addAFile("E2", E);
E3 = addAFile("E3", E);
}
private DefaultMutableTreeNode addAFile(String fileName, DefaultMutableTreeNode parentFolder) {
DefaultMutableTreeNode newFile = new DefaultMutableTreeNode(fileName);
parentFolder.add(newFile);
return newFile;
}
你可以在初始化樹相關組件(clonedTree,原始樹,它們的模型和它們添加的面板)時添加代碼嗎? – Slimu
如果我用相同的模型創建一個新的JTree,它會渲染原始樹。不知道爲什麼它不能與clonedTree一起工作。 –
從你的代碼我看你只顯示克隆的樹。你爲什麼要用2棵樹? – Slimu