2010-06-18 83 views
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++; 
    } 
} 

回答

2

我想你可以使用setSelectedRow[1]功能。

編輯:添加一個草圖解決

你需要有一個tree model,將讀取文件系統(原source)的文件:

import java.io.File; 
import java.util.Enumeration; 
import java.util.Iterator; 
import java.util.Vector; 
import javax.swing.event.TreeModelEvent; 
import javax.swing.event.TreeModelListener; 
import javax.swing.tree.DefaultTreeModel; 
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 File(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 void myButtonActionPerformed(java.awt.event.ActionEvent evt) { 
    File f = new File(MY_DIR + "/file" + new Random().nextInt()); 
    try { 
     f.createNewFile(); 

     FileSystemModel model = new FileSystemModel(new File(MY_DIR)); 
     tree.setModel(model); 

     File root = (File) tree.getModel().getRoot(); 
     TreePath path = getPathFor(model, root, f); 

     tree.expandPath(path); 
     tree.setSelectionPath(path); 
    } 
    catch (IOException e) 
    { 

    } 
} 

最後,您將返回TreePath爲新創建的文件:

private TreePath getPathFor(FileSystemModel model, File root, File searched) 
{ 
    TreePath path = getPath(model, null, root, searched); 

    return path; 
} 

private TreePath getPath(FileSystemModel model, TreePath path, File parent, File searched) 
{ 
    if (path == null) 
    { 
     path = new TreePath(parent); 
    } 
    else if (parent.isDirectory()) 
    { 

     path = path.pathByAddingChild(parent); 
    } 

    if (parent.getAbsolutePath().equals(searched.getAbsolutePath())) 
    { 
     return path.pathByAddingChild(parent); 
    } 

    for (int i = 0; i < model.getChildCount(parent); i++) 
    { 
     File child = ((File)model.getChild(parent, i)).getAbsoluteFile(); 
     TreePath found = getPath(model, path, child, searched); 

     if (found != null) 
     { 
      return found; 
     } 
    } 

    return null; 
} 

這僅僅是你如何能做到這一點演示,但它的高度inneficient,因爲它每次都重新創建模型。我相信你可以想出更好的解決方案。

+0

你能詳細說明嗎? – user236501 2010-06-18 11:47:27

+0

如何將新文件設置爲第一行,文件按alphabert排序 – user236501 2010-06-18 12:51:51

+0

我將很快畫出一個示例 – 2010-06-18 13:41:07

相關問題