java - JTree - file system viewer tree only with fixed file extensions -
i'm in process of writing school project , have 1 problem can't solve. have tree shows directories , file paths need display .mp3 , .wav files. can give me advice how that? have class tree model:
public boolean isleaf(object node) { file file = (file) node; homecoming file.isfile(); } public int getindexofchild(object parent, object child) { file directory = (file) parent; file file = (file) child; string[] children = directory.list(); (int = 0; < children.length; i++) { if (file.getname().equals(children[i])) { homecoming i; } } homecoming -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() { homecoming getname(); } } string getfiledetails(file file) { if (file == null) homecoming ""; stringbuffer buffer = new stringbuffer(); buffer.append("name: " + file.getname() + "\n"); buffer.append("path: " + file.getpath() + "\n"); buffer.append("size: " + (double)(file.length()/1024)/1024 + " mb" + "\n"); homecoming buffer.tostring(); } } string[] children = directory.list();
wherever listing children, in above, know file.list() can take filenamefilter. there similar method called file.listfiles() can take filefilter (very similar). using 1 of these, can filter files filename (which includes extension).
java file system jtree
No comments:
Post a Comment