java - 如何在java中迭代用户首选项的节点

标签 java

在我的简单 swing 应用程序中,我创建了几个文本框,用户在其中输入应用程序将使用的文件夹的文件夹路径。这些是使用 Preferences API 存储的。

目前,我必须将文件夹路径以及首选项存储在 ArrayList 中,然后遍历 ArrayList,当我应该能够遍历用户时,这似乎有点毫无意义文件夹名称的首选项。如何检索特定节点上用户的所有首选项并迭代它们。

我的代码:

对于偏好设置的收集:

public ArrayList<String> folderList() throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException{
        ArrayList<String> AL_folderList=new ArrayList<String>();        
        String PathForBRAVO=null;
        String PathForImpedance=null;
        String PathForHRM=null;
        String PathForDiagnosis=null;
        String PathForBreath=null;

        if(txtPathForBRAVO!=null){
            prefs.put(PathForBRAVO, txtPathForBRAVO.getText());
        AL_folderList.add(txtPathForBRAVO.getText());
        }
        if(txtPathForImpedance!=null){
            prefs.put(PathForImpedance, txtPathForImpedance.getText());
        AL_folderList.add(txtPathForImpedance.getText());
        }
        if(txtPathForHRM!=null){
            prefs.put(PathForHRM, txtPathForHRM.getText());
        AL_folderList.add(txtPathForHRM.getText());
        }
        if(txtPathForDiagnosis!=null){
            prefs.put(PathForDiagnosis, txtPathForDiagnosis.getText());
        AL_folderList.add(txtPathForDiagnosis.getText());
        }
        if(txtPathForBreath!=null){
            prefs.put(PathForBreath, txtPathForBreath.getText());
        AL_folderList.add(txtPathForBreath.getText());
        }           
        Iterator.main(null);
        return AL_folderList;               
    }

这被传递给迭代器

public static void main(String[] args) throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException {      


        Preferences userPrefs = Preferences.userNodeForPackage(TBB_SQLBuilder.class);
        <<<<<<<<<<<<<<< How to iterate through the Preferences node holding PathForBreath,PathForHRM etc rather than having to iterate through the returned ArrayList from the folderList method????
}

最佳答案

您可以简单地使用 keys() 迭代键。 Preferences 的方法。

public static void main(String[] args) throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException {      


        Preferences userPrefs = Preferences.userNodeForPackage(TBB_SQLBuilder.class);

        String[] keys = userPrefs.keys();

        for(int i=0; i<keys.length; i++){

           String value = userPrefs.get(key, "No value for this key");

        }

}

关于java - 如何在java中迭代用户首选项的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37851536/

相关文章:

java - 在保留 xsi :type 的同时合并文档

java - 实现 Kruskal 算法时测试电路

java - Wasabi自动重启docker容器

Java - 在没有窗口的情况下将组件或图像绘制到屏幕上

java - 如何在 Java 中将 JSON 架构中的默认值添加到 JSON 文档

java - 无法使用 HTMLParser 获取网页的所有内容

Java:使用局部变量的匿名内部类

java - Java 中的 OpenPGP 公钥导入

java - 将 ActionListener 添加到 JRadioButton 和 JComboBox

java - hibernate 异常 : Found two representations of same collection