这是问题所在:
我有一个主面板,它有 3 个子面板,其中包含一个状态更新区域、一个 projectlistTree 面板和一个分析面板。
项目列表树面板需要两个类:
- 一个包含用户界面组件的面板类。
- 为 jtree 提供树模型的树列表模型类。
我试过的:
创建并保存新项目后,树应该会更新,它应该出现在 projectlisttree 面板上。
基于 SO 上的许多答案,我创建了一个方法集并获取树模型,并且在保存项目时,再次设置模型。基本上 jtree 树模型是使用我创建的 getmodel 方法设置的。
模型应该如何设置和调用?
这是 set 和 get treemodel 方法:
以下 set 和 get 方法在类 common.java 中
public FileSystemModel getFsModel() {
return fsModel;
}
public void setFsModel(FileSystemModel fsModel) {
this.fsModel = fsModel;
}
这是树模型类
import java.io.File;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
class FileSystemModel implements TreeModel {
private File root;
private Vector listeners = new Vector();
public FileSystemModel(File rootDirectory) {
root = rootDirectory;
}
public Object getRoot() {
return root;
}
public Object getChild(Object parent, int index) {
File directory = (File) parent;
String[] children = directory.list();
return new TreeFile(directory, children[index]);
}
public int getChildCount(Object parent) {
File file = (File) parent;
if (file.isDirectory()) {
String[] fileList = file.list();
if (fileList != null)
return file.list().length;
}
return 0;
}
public boolean isLeaf(Object node) {
File file = (File) node;
return file.isFile();
}
public int getIndexOfChild(Object parent, Object child) {
File directory = (File) parent;
File file = (File) child;
String[] children = directory.list();
for (int i = 0; i < children.length; i++) {
if (file.getName().equals(children[i])) {
return i;
}
}
return -1;
}
public void valueForPathChanged(TreePath path, Object value) {
File oldFile = (File) path.getLastPathComponent();
String fileParentPath = oldFile.getParent();
String newFileName = (String) value;
File targetFile = new File(fileParentPath, newFileName);
oldFile.renameTo(targetFile);
File parent = new File(fileParentPath);
int[] changedChildrenIndices = { getIndexOfChild(parent, targetFile) };
Object[] changedChildren = { targetFile };
fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);
}
private void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) {
TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
Iterator iterator = listeners.iterator();
TreeModelListener listener = null;
while (iterator.hasNext()) {
listener = (TreeModelListener) iterator.next();
listener.treeNodesChanged(event);
}
}
public void addTreeModelListener(TreeModelListener listener) {
listeners.add(listener);
}
public void removeTreeModelListener(TreeModelListener listener) {
listeners.remove(listener);
}
private class TreeFile extends File {
public TreeFile(File parent, String child) {
super(parent, child);
}
public String toString() {
return getName();
}
}
}
Jtree 面板:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package analysisui;
import java.awt.BorderLayout;
import java.io.File;
import javax.swing.JPanel;
import javax.swing.JTree;
public class ShowProjectTree extends JPanel {
private FileSystemModel fileSystemModel;
Common commonCls;
/**
* Creates new form ShowProjectTree
*/
public ShowProjectTree(Common cmnClas) {
commonCls = cmnClas;
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
fileSystemModel = commonCls.getFsModel();
proListTree = new javax.swing.JTree(fileSystemModel);
revalidate();
jScrollPane1.setViewportView(proListTree);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree proListTree;
// End of variables declaration
}
因此,一旦我将一个新项目保存到项目文件夹中,就会调用 set model 方法。
commonCls.setAppendMessage("\n>>"+projectName + " created \n");
revalidate();