【问题标题】:Putting properties file into a JTree rather than just displaying it将属性文件放入 JTree 而不仅仅是显示它
【发布时间】:2014-10-13 12:45:21
【问题描述】:

这是我当前的代码 - 它只显示我的 GitCommands.properties 文件中的密钥对值并将其显示在我的 GetAllProperties.java 文件中 - 是否可以对其进行排序,使其进入 JTree 而不仅仅是显示它列表类型格式?

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class GetAllProperties {
    private static JTree tree;

    public static void main(String[] args) {

        Properties properties = new Properties();
        try {
            String filename = "GitCommands.properties";
            // File file = new
            // File("/ApplicationTest/.settings/GitCommands.properties");
            FileInputStream fileInputStream = new FileInputStream(filename);

            // load properties file
            properties.load(fileInputStream);

            System.out.println("keys avialble in Properties Files are:");
            System.out.println(properties.keySet());

            System.out.println("Key Value Pairs :");
            Enumeration enumeration = properties.keys();
            while (enumeration.hasMoreElements()) {
                String key = (String) enumeration.nextElement();
                System.out.println(key + ": " + properties.get(key));
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

【问题讨论】:

  • 我不确定树是否有意义,但有可能。看看How to Use TreesJTable 可能更有意义,请参阅 How to Use Tables
  • 这可能看起来有点奇怪,但我不允许使用一张桌子,它必须是一棵树:(你介意指点我正确的方向吗?我看了很多示例,但尚未遇到将属性文件放入 JTree 的示例,感谢您的回复!
  • 嗯,这就是问题所在,什么是节点,什么是叶子?
  • 我想要一个名为 GIT commands 的根文件,当单击它时,它会打开以将所有 GIT 命令键显示为节点。然后单击每个单独的节点后,我希望它将同一属性文件中的描述字符串显示到右侧的面板中

标签: java swing jtree properties-file


【解决方案1】:

您可以使用树模型简单地填充树

DefaultMutableTreeNode root = new DefaultMutableTreeNode(filename);
DefaultTreeModel treeModel = new DefaultTreeModel(root);

Enumeration enumeration = properties.keys();  
while (enumeration.hasMoreElements()) {  
    String key = (String) enumeration.nextElement();
    String nodeObj = key+" : "+properties.get(key);
    treeModel.insertNodeInto(new DefaultMutableTreeNode(nodeObj), root, 0);
}

JTree tree = new JTree(treeModel);

注意:元素未排序...为此,您需要将所有键放入列表并对该列表进行排序

List sortedList<String> = new ArrayList<String>();
Enumeration enumeration = properties.keys();  
while (enumeration.hasMoreElements()) {  
    String key = (String) enumeration.nextElement();
    sortedList.add(key);
}
Collection.sort(sortedList);

for(String key: sortedList){
    String nodeObj = key+" : "+properties.get(key);
    // [...] same as above
}

【讨论】:

  • 啊,谢谢!我已经填充了我的jtree,但不是为了我想知道如何去做,谢谢你的帮助! ^.^
  • 谢谢!终于开始有点意思了哈哈 :)
  • 我在代码上遇到错误,说 { 应该在 Enumeration enumeration = properties.keys(); 之后在代码的第一部分,如果我把 { 放在里面,那么下划线 "collection" Collection.sort(sortedList);和 "sortedList" for(String key: sortedList){ .... 你知道如何解决这个问题吗? :)
  • 它应该是使用这个 java.util.Collections 的“Collections”;我错过了一个's',对不起
  • 啊,我不敢相信我没有意识到自己,原来是这样,哈哈,谢谢!