Jtree does not update when starting GUI

Asked

Viewed 41 times

1

I’m having a problem that I’m not being able to visualize the solution:

Simply put, my Java program has a swing GUI that contains a Jtree (Treemodel customized for system directory view ) There is a button to select a directory (via Jfilechooser), after selecting the Jtree is updated and the path of that directory is stored in an external control file (.txt) Every time you start the program, this file should be read and if there is a path. Jtree is populated from this path.

After selecting via Jfilechooser jTree populates normally and writing to . txt is occurring. But when I try to load the path from the external file Jtree is not being populated. The reading of the file is occurring because when I have the variable’s value printed on the console, the value of the path is there - Example: If the contents of . txt is C: testSync Folderb. In the GUI class constructor after targetDirectory = new File(synchronizer.readDstFile() ); have it printed on the console targetDirectory.getPath()

When I run the program the console prints C: testSync Folderb . That is, the value in the variable that goes to Jtree is right, which may be wrong??

Class myGUI:

private Synchronizer synchronizer;
private File targetDirectory;
/...

public myGUI(){
    super("SyncBox");
    synchronizer = new Synchronizer();  
    targetDirectory = new File(synchronizer.readDstFile() );
    initGUI();
}

public void initGUI(){
   //...
   subPanelCenter.add( getScrlTargetTree() );
  //...
}

 /**
 * Return the JTree object of the target directory
 * @return JTree - of the target directory
 */
 private JTree getTargetTree() {  
    if (jTreeTarget == null) {  
        targetDirectoryExplorerModel = new DirectoryExplorerModel( targetDirectory );
        jTreeTarget = new JTree( targetDirectoryExplorerModel );
        jTreeTarget.setRootVisible(false);  
        jTreeTarget.setShowsRootHandles(true);  
    }  
    return jTreeTarget;  
 } 


/**
 * Initializing JScrollPane component for target directory
 * It will nest the jTreeTarget
 * */
private JScrollPane getScrlTargetTree() {
    if (jScrlTargetTree == null) {
        jScrlTargetTree = new JScrollPane();
        jScrlTargetTree.setViewportView( getTargetTree() );
    } 
    return jScrlTargetTree;
}

//refresh tree
private void refreshUI() {
    jTreeTarget.setModel(new DirectoryExplorerModel(targetDirectory));
}


/**
* choosing Target Directory (call JFileChooser)
*/
private void chooseTargetDirectory(){
     JFileChooser fileChooser = new JFileChooser();
     fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
     fileChooser.setCurrentDirectory(targetDirectory);
     int ret = fileChooser.showOpenDialog(this);
     if (ret == JFileChooser.APPROVE_OPTION) {
        targetDirectory = fileChooser.getSelectedFile(); 
     }
 }
//...

public void actionPerformed(ActionEvent evt){

    if( evt.getSource() == btSelectTargetDirectory ){
        chooseTargetDirectory();
        synchronizer.createControlFileDst(targetDirectory);
        updateTargetPathLabel();
        refreshUI(); //Refresh trees
    }

    //...
}

Synchronizer class

//...

/**
 * Reads the path to Target directory in the control file (.txt)
 * @return String - directory path
 */
public String readDstFile() {
    try {
        File file = new File("ControlFiledst.txt");
        StringBuilder sb = new StringBuilder();
        String s = "";
        if (file.exists()){
            BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
            while ((s = br.readLine()) != null) {
                sb.append(s + "\n");
            }
            br.close();
        }
        String str = sb.toString();
        return str;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Thank you in advance :)

  • Like that? http://stackoverflow.com/q/12697650/4056678

1 answer

1


I found out and solved the problem I was having

the line sb.append(s + "\n"); in readDstFile()

The \n was invisibly messing up my format string

So when I called targetDirectory = new File(synchronizer.readDstFile() ); the builder of File did not accept the standard. I discovered this by having it printed on the next line targetDirectory.exists(), who was returning false

The solution was simply sb.append(s);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.