Read CSV file and add the fields to a JavaFX tableview

-1

I'm trying to read a file with CSV format which has movie information through a Filereader, add all the elements in a list and then display them in a Tableview in JavaFX. The problem is that I do not execute my method.

'/ *  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  * / package act8;

import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable;

import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.stage.FileChooser; import javafx.stage.Stage; import javax.swing.JOptionPane;

/ **  *  * @author Anton  * / public class FXMLDocumentController implements Initializable {     Scene scene;     Stage window;     Scanner sc = new Scanner (System.in);     FileChooser fs;

@FXML 
private TextField txtdatosd;
@FXML 
private TextField txtdatosr;
@FXML
private Button btnAbrirD;
@FXML
private Button btnAbrirR;
@FXML
private Button btncargard;
@FXML
private TableView tbldisponibles;

public void abrirD() throws IOException{
    File f=null;
    f=fs.showOpenDialog(window);
        if(f != null){
            txtdatosd.setText(f.getAbsolutePath());
        }

}

public void leerArchivoCSVd() throws FileNotFoundException, IOException{
String csvFile = (txtdatosd.getText());

BufferedReader br = null; String line=""; // Define separator "," String cvsSplitBy=","; try {     br = new BufferedReader (new FileReader (csvFile));     while ((line = br.readLine ())! = null) {
        String [] data = line.split (cvsSplitBy);         // Print data.        System.out.println (data [0] + "," + data [1] + "," + data [2] + "," + data [3] + "," + data [4]);     } } catch (FileNotFoundException e) {     e.printStackTrace (); } catch (IOException e) {     e.printStackTrace (); } finally {     if (br! = null) {         try {             br.close ();         } catch (IOException e) {             e.printStackTrace ();         }     } }     }

public void escucha(){
    btnAbrirD.setOnAction(evt ->{try {
        abrirD();
        } catch (IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }

});

}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    escucha();
}    

} '

    
asked by Antonio 11.11.2018 в 01:53
source

1 answer

0

To read any plain text file I suggest this code.

public Stream<String> leerAchivoExterno(String rutaArchivo) {
    Stream<String> lineas = null;

    try {
        lineas = Files.lines(Paths.get(rutaArchivo), Charset.forName("UTF-8"));
    } catch (IOException ex) {
        Logger.getLogger(ArchivoImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    return lineas;
}

then to add it to a table (TableView), you must do several things:

  • First create a class with the structure of the fields in the table
  • Ejm: Format for a table with a single column called description

    public class FormatoDescripcion {
    private String descripcion;
    
    public FormatoDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
    
    public String getDescripcion() {
        return descripcion;
    }
    
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
    

    }

  • Then you must initialize the column of the table and the table with this class

    @FXML
    private ObservableList<FormatoDescripcion> datos;
    @FXML
    private TableColumn<FormatoDescripcion, String> descripcion;    
    @FXML
    private TableView<FormatoDescripcion> tablaDescripcion;
    
  • Initialize the values

    public void initialize(URL url, ResourceBundle rb) {
     datos = FXCollections.observableArrayList();
    
     descripcion.setCellValueFactory(new PropertyValueFactory<>  ("descripcion"));
    
    tablaDescripcion.setItems(datos);
       }
    
  • Add the values you want dynamically. This would be about the data variable

  • ejm:

    datos.add(new FormatoDescripcion("texto de interes"));
    
        
    answered by 26.11.2018 в 01:24