将属性文件放入JTree中,而不只是显示它 [英] Putting properties file into a JTree rather than just displaying it

查看:112
本文介绍了将属性文件放入JTree中,而不只是显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my current code - it just displays the key pair values from my GitCommands.properties file and displays it in my GetAllProperties.java file - is it possible to sort it so it goes in to a JTree rather than just displaying it in a list type format?

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());
        }
    }
}

推荐答案

您可以使用树模型轻松填充树

you can simply populate the tree using the tree model

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);

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

NOTE: the elements are not sorted... to do so, you need to push all keys into a list and sort that list

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中,而不只是显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆