I have this problem, when a run my application I Cant see the elements Than I before added to my table, I create a Class (Personas) and use the PropertyValueFactory. thanks and sorry for the error in the language, I speak Spanish. This is the code:
package ejemplo.tableview;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class EjemploTableView extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<Personas> data = FXCollections.observableArrayList(
new Personas("Diego","Maradona"),
new Personas("Lionel","Messi")
);
TableView<Personas> tabla = new TableView();
TableColumn<Personas,String> c1 = new TableColumn("Nombre");
c1.setMinWidth(200d);
c1.setCellValueFactory(new PropertyValueFactory<Personas,String>("nombre"));
TableColumn<Personas,String> c2 = new TableColumn("Apellido");
c2.setMinWidth(200d);
c2.setCellValueFactory(new PropertyValueFactory<>("apellido"));
tabla.getColumns().addAll(c1,c2);
tabla.setItems(data);
StackPane root = new StackPane();
root.getChildren().add(tabla);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
this is the class Personas:
package ejemplo.tableview;
public class Personas {
private String nombre;
private String apellido;
public Personas(String nombre,String apellido){
this.nombre = nombre;
this.apellido = apellido;
}
}