I am a beginner in programming. I wanted to create total commander aplication. I had no bigger problem but that one i can not solve.
To print my tree i use function getChild() from my class TreeConstructor that implements from JTreeModel. It works great but it prints all path to file or directory not only file name. I thought about creating myJTree class that will extend from JTree class and override function that prints the node but acctually i do not know where is this function and her name.
class TreeConstructor implements TreeModel {
//To constructor we need to give path
//From where it it starts painting a tree
protected File root;
public TreeConstructor(File root) { this.root = root; }
//Giving root of a tree
@Override
public Object getRoot() {
return root;
}
//Function to change root
public void rootChanger(Object parent) {
this.root = (File)parent;
}
@Override
public Object getChild(Object parent, int index) {
String[] children = ((File)parent).list();
if((children.length<=index)||(children == null))return null;
return new File((File)parent,children[index]);
}
@Override
public int getChildCount(Object parent) {
String[] children = ((File)parent).list();
if(children == null) return 0;
return children.length;
}
@Override
public boolean isLeaf(Object node) { return ((File)node).isFile(); }
@Override
public int getIndexOfChild(Object parent, Object child) {
String[] children = ((File)parent).list();
if (children == null) return -1;
String childname = ((File)child).getName();
for(int i = 0; i < children.length; i++) {
if (childname.equals(children[i])) return i;
}
return -1;
}
@Override
public void valueForPathChanged(TreePath path, Object newValue) {}
@Override
public void addTreeModelListener(TreeModelListener l) {}
@Override
public void removeTreeModelListener(TreeModelListener l) {}
It is my application: https://i.stack.imgur.com/6AsiV.jpg
I will be grateful for your help Daniel