简体   繁体   English

如何解决 JTree 中的显示问题?

[英]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.为了打印我的树,我使用了从 JTreeModel 实现的类 TreeConstructor 中的函数 getChild()。 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.我想过创建 myJTree 类,该类将从 JTree 类扩展并覆盖打印节点的函数,但实际上我不知道这个函数和她的名字在哪里。

class TreeConstructor implements TreeModel {类 TreeConstructor 实现 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这是我的应用程序: 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) 目录中的 Java JTree(显示完整路径而不仅仅是文件名)

  1. Write your own renderer编写自己的渲染器

Depending on your implementation it may be the easiest way to just write your own Render根据您的实现,它可能是编写自己的 Render 的最简单方法

https://www.logicbig.com/tutorials/java-swing/jtree-renderer.html 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.此外,必须实现 TreeModel 接口中定义的方法以提供 JTree 的正确工作。 Especially add/removeTreeModelListener.特别是添加/删除TreeModelListener。 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 ?这是我使用 JTree 的代码,我可以使用您的代码进行渲染吗? I do not understand it :/我不明白 :/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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