在我的简单 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/