I am new using JavaFX and have been having problems with a project. The project view has a TableView
which should show all the files within a folder. The information is stored both in a text file (in order to detect possible registration errors) and in a ObservableList
to then process the information to fill the table. In the text file everything is shown, while in the table the records are shown empty. I add the code of my project.
Class code FileOfProyect
package pruevanavegadorfxml;
import javafx.beans.property.SimpleStringProperty;
public class FileOfProyect {
private final SimpleStringProperty name;
private final SimpleStringProperty size;
private final SimpleStringProperty path;
public FileOfProyect(String name, String size, String path){
this.name=new SimpleStringProperty(name);
this.size=new SimpleStringProperty(size);
this.path=new SimpleStringProperty(path);
}
String getName(){ return(name.get()); }
String getSize(){ return(size.get()); }
String getPath(){ return(path.get()); }
void setName(String nameg){ name.set(nameg); }
void setSize(String sizeg){ size.set(sizeg); }
void setPath(String pathg){ path.set(pathg); }
}
Code of the main class
package pruevanavegadorfxml;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author ANGEL
*/
public class Pruevanavegadorfxml extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Code of the controller
package pruevanavegadorfxml;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
/////////////////////******************************************************
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
////////////////////////////////////////////// IMPORTS NAVEGADOR////////////////////////////////////
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.util.StringConverter;
import javax.swing.event.ChangeListener;
import javax.swing.tree.DefaultMutableTreeNode;
/**
*
* @author ANGEL
*/
public class FXMLDocumentController implements Initializable {
@FXML ScrollPane navegador;
@FXML private Label label;
VBox ficheros = new VBox();
final ObservableList<FileOfProyect> listItems = FXCollections.observableArrayList();
TableView<FileOfProyect> listaarchivos;
/////////////////////*********************************************************
File misarchivos;
BufferedWriter bw;
FileWriter fw;
@Override
public void initialize(URL url, ResourceBundle rb) {
listaarchivos = new TableView<>>();
/////////////////////********************************************
navegador.setContent(ficheros);
navegador.setFitToHeight(true);
navegador.setFitToWidth(true);
ficheros.getChildren().add(listaarchivos);
listaarchivos.setItems(listItems);
misarchivos = new File("src\pruevanavegadorfxml\misarchivos.txt");
if(misarchivos.exists())
misarchivos.delete();
try {
misarchivos.createNewFile();
misarchivos.setWritable(true);
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
try {
fw = new FileWriter(misarchivos.getAbsoluteFile());
bw = new BufferedWriter(fw);
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
//for(int i=0;i<File.listRoots().length;i++){
//buscararchivos(File.listRoots()[i].toString());
buscararchivos("C:\Users\ANGEL\Desktop");
//}
///////////////////////////////////// COLUMNA DE NOMBRES <FileOfProyect,String>
TableColumn<FileOfProyect,String> nameCol = new TableColumn<>("Nombre");
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
///////////////////////////////////// COLUMNA DE TAMAÑOS
TableColumn<FileOfProyect,String> sizeCol = new TableColumn<>("Tamaño");
sizeCol.setCellValueFactory(new PropertyValueFactory<>("size"));
///////////////////////////////////// COLUMNA DE RUTAS
TableColumn<FileOfProyect,String> pathCol = new TableColumn<>("Ruta");
pathCol.setCellValueFactory(new PropertyValueFactory<>("path"));
listaarchivos.getColumns().addAll(nameCol, sizeCol, pathCol);
try {
bw.close();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void buscararchivos(String archivo){
File folder = new File(archivo+"\");
System.out.println("Explorando la carpeta: "+folder);
File[] contenido = folder.listFiles();
if(contenido != null){
for (File contenido1 : contenido) {
String auxString = contenido1.toString();
File auxFile = new File(auxString);
if(auxFile.isDirectory()){
buscararchivos(auxFile.getAbsolutePath());
}else{
FileOfProyect auxFOP = new FileOfProyect(auxFile.getName(),String.valueOf(auxFile.length()),auxFile.getPath());
listItems.add(auxFOP);
try {
bw.write(auxFOP.getName()+"\t"+auxFOP.getPath()+"\t"+auxFOP.getSize());
bw.newLine();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}else{
System.out.println("La carpeta "+folder+" esta vacio");
}
}
}
File code FXML