I have a problem that I can not see the solution:
Simply put, my Java program has a swing GUI that contains a JTree (TreeModel custom for system directory view) There is a button to select a directory (via JFileChooser), after selecting 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 must be read and the path exists. JTree is populated from that path.
After selecting via JFileChooser the jTree populates normally and writing to .txt is occurring. But when I try to load the path from the external file the JTree is not being populated. The file reading is occurring because when I command to print the value of the variable in the console, the value of the path is there - Example: If the contents of the .txt is C: \ testSync \ FolderB.
In the constructor of the GUI class after targetDirectory = new File(synchronizer.readDstFile() );
command to print to console targetDirectory.getPath()
When you run the program, the console prints C: \ testSync \ FolderB. That is the value in the variable that goes to JTree is right, what can go wrong ??
myGUI class:
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 :)