简体   繁体   中英

How can I solve problem with display in JTree?

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

Without further code example i suggest you look for the following:

  1. Use the search function, you are not the first one with this problem :)

Java JTree from directory (shows full path instead of just the name of the file)

  1. Write your own renderer

Depending on your implementation it may be the easiest way to just write your own Render

https://www.logicbig.com/tutorials/java-swing/jtree-renderer.html

As it already said, you need your own renderer. Something like this:

public class FileTreeRenderer extends DefaultTreeCellRenderer {
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
            boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        if (value instanceof File) {
            setText(((File) value).getName());
        }
        return this;
    }

}

And then you need to set the renderer into your tree

tree.setCellRenderer(new FileTreeRenderer());

Also the methods defined in the TreeModel interface must be implemented to provide correct working of your JTree. Especially add/removeTreeModelListener. They are used to provide a possibility to notify tree when your model is changed (for example when user renames a node).

public class Panels extends JPanel{

    File f1 = new File("C:\\Users");

    public TreeConstructor model = new TreeConstructor(f1);

    public JTree tree = new JTree(model);

    public JTextField pathPlace = new JTextField(100);

    JScrollPane scrollPane = new JScrollPane(tree);

    Panels(){

        pathPlace.setText("C:\\Users");

        tree.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==2) {      
                    System.out.println();
                    repaintingTree(pathPlace, tree, model);
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });

        pathPlace.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                repaintingTree(pathPlace, tree, model, pathPlace.getText());

            }
        });

        setLayout(new BorderLayout());
        add(scrollPane);
        add(pathPlace,BorderLayout.NORTH);
    }

    public static void repaintingTree(JTextField pathPlace, JTree tree, TreeConstructor model){
        File place = new File(tree.getLastSelectedPathComponent().toString());
        if(place.isDirectory()) {
            pathPlace.setText(tree.getLastSelectedPathComponent().toString());
            model = new TreeConstructor(place);
            tree.setModel(model);
            tree.repaint();
    }}

    public static void repaintingTree(JTextField pathPlace, JTree tree, TreeConstructor model, String path){
        File place = new File(path);
        if(place.isDirectory()) {
            pathPlace.setText(path);
            model = new TreeConstructor(place);
            tree.setModel(model);
            tree.repaint();
            }
        else {
            JOptionPane.showMessageDialog(null, "Wrong directory");
        }
    }

here is code where i use JTree can I use your code to rendering with this ? I do not understand it :/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM