I am trying to populate a tableview with Javafx, the database test.db contains warehouses table with three fields, "id", "name", and "capacity". The following code is a sample program
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.TableColumn;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class javafxsqliteconntable extends Application {
@Override
public void start(Stage primaryStage){
primaryStage.setTitle("Que onda");
Label etiqueta = new Label("Hola Mundo");
Button boton = new Button("Presionar aqui");
TableView tableView = new TableView();
TableColumn<Integer, Person> column1 = new TableColumn<>("Id");
column1.setCellValueFactory(new PropertyValueFactory<>("id"));
TableColumn<String, Person> column2 = new TableColumn<>("name");
column2.setCellValueFactory(new PropertyValueFactory<>("Name"));
TableColumn<Double, Person> column3 = new TableColumn<>("capacity");
column3.setCellValueFactory(new PropertyValueFactory<>("Capacity"));
tableView.getColumns().add(column1);
tableView.getColumns().add(column2);
tableView.getColumns().add(column3);
VBox vbox = new VBox(20, etiqueta, boton, tableView);
ObservableList<Person> data = FXCollections.observableArrayList();
String sql = "SELECT id, name, capacity FROM warehouses";
try (Connection conn = this.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)){
while (rs.next()) {
System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getDouble("capacity"));
data.addAll(new Person(rs.getInt("id"), rs.getString("name"), rs.getDouble("capacity")));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
Scene scene = new Scene(vbox, 300, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
public class Person {
private int Id;
private String Nombre = null;
private Double Capacidad = null;
public Person(int id, String nombre, Double capacidad) {
this.Id=id;
this.Nombre = nombre;
this.Capacidad = capacidad;
}
public int getId(){
return Id;
}
public String getnombre() {
return Nombre;
}
public Double getcapacidad() {
return Capacidad;
}
}
private Connection connect() {
String url ="jdbc:sqlite:test.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public static void main(String[] args) {
launch(args);
}
}
when i compiled i do not have any kind of errors, the sqlite connection is succesfully established, and the line
System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getDouble("capacity"));
gives the information correctly in the console command line but
data.addAll(new Person(rs.getInt("id"), rs.getString("name"), rs.getDouble("capacity")));
does not give me anything in the Tableview, am I missing something?, i made the observablelist and i already have the class person inside the program. anyhelp is appreciated in order to get the data populated in tableview javafx Thank you in advance,any help is appreciated