问题在于:
我有一个主面板,它有3个子面板,包含状态更新区域,projectlistTree面板和分析面板。
项目列表树面板需要两个部分:
我尝试过:
创建并保存新项目后,应该更新树,它应该出现在projectlisttree面板上。
基于SO上的许多答案,我创建了一个方法集并获取了treemodel,并且在保存项目时,再次设置模型。基本上jtree树模型是使用我创建的getmodel方法设置的。
如何设置和调用模型?
这是set和get treemodel方法:
以下set和get方法在class common.java
中 public FileSystemModel getFsModel() {
return fsModel;
}
public void setFsModel(FileSystemModel fsModel) {
this.fsModel = fsModel;
}
这是treemodel类
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();