0
我想讓它變成當我單擊按鈕時,它會創建一個新文件。然後jTree將突出顯示新文件。以下是我的代碼。目前我創建新文件,我將顯示新文件但不突出顯示文件。如何在JTree中突出顯示新創建的文件
class FileTreeModel implements TreeModel {
private FileNode root;
public FileTreeModel(String directory) {
root = new FileNode(directory);
}
public Object getRoot() {
return root;
}
public Object getChild(Object parent, int index) {
FileNode parentNode = (FileNode) parent;
return new FileNode(parentNode, parentNode.listFiles()[index].getName());
}
public int getChildCount(Object parent) {
FileNode parentNode = (FileNode) parent;
if (parent == null || !parentNode.isDirectory()
|| parentNode.listFiles() == null) {
return 0;
}
return parentNode.listFiles().length;
}
public boolean isLeaf(Object node) {
return (getChildCount(node) == 0);
}
public int getIndexOfChild(Object parent, Object child) {
FileNode parentNode = (FileNode) parent;
FileNode childNode = (FileNode) child;
return Arrays.asList(parentNode.list()).indexOf(childNode.getName());
}
public void valueForPathChanged(TreePath path, Object newValue) {
}
public void addTreeModelListener(TreeModelListener l) {
}
public void removeTreeModelListener(TreeModelListener l) {
}
}
class FileNode extends java.io.File {
public FileNode(String directory) {
super(directory);
}
public FileNode(FileNode parent, String child) {
super(parent, child);
}
@Override
public String toString() {
return getName();
}
}
jTree = new JTree();
jTree.setBounds(new Rectangle(164, 66, 180, 421));
jTree.setBackground(SystemColor.inactiveCaptionBorder);
jTree.setBorder(BorderFactory.createTitledBorder(null, "",
TitledBorder.LEADING, TitledBorder.TOP, new Font("Arial",
Font.BOLD, 12), new Color(0, 0, 0)));
FileTreeModel model = new FileTreeModel(root);
jTree.setRootVisible(false);
jTree.setModel(model);
expandAll(jTree);
public void expandAll(JTree tree) {
int row = 0;
while (row < tree.getRowCount()) {
tree.expandRow(row);
row++;
}
}
你能詳細說明嗎? – user236501 2010-06-18 11:47:27
如何將新文件設置爲第一行,文件按alphabert排序 – user236501 2010-06-18 12:51:51
我將很快畫出一個示例 – 2010-06-18 13:41:07