2016-06-16 7 views
0

In meiner einfachen Swing-Anwendung habe ich ein paar Textfelder erstellt, in denen ein Benutzer die Ordnerpfade für Ordner eingibt, die die Anwendung verwenden wird. Diese werden mit der Preferences API gespeichert.Wie durchlaufen Knoten der Benutzereinstellungen in Java

Im Moment habe ich die Ordner-Pfade in einem Arraylist sowie die Einstellungen zu speichern habe und dann durch die Arraylist iterieren, die ein bisschen sinnlos scheint, wenn ich sollte der Lage sein, für Ordnernamen der Benutzer-Einstellungen zu durchlaufen. Wie kann ich alle Einstellungen für den Benutzer an einem bestimmten Knoten abrufen und durchlaufen Sie sie.

Mein Code:

Für die Sammlung der Vorlieben:

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

Dies Iterator

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???? 
} 

Antwort

1

Sie einfach durchlaufen kann mit dem keys() Verfahren über die Tasten, übergeben wird von 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"); 

     } 

} 
Verwandte Themen