I have a tableview where I display data from Alumno(name,lastname...). I have differents columns where I show each field from Alumno and all works fine. Also, I want to add a new column and put inside cell a button, where user click on the button, a window appear showing info about selected item (aka, selected person).
I try this but I don't what's wrong, because buttons appear but item always return null:
Class Alumno
class Alumno
{
private String nombre;
private String apellidos;
getters & setters
}
Class where I show data into table
public class AlumnosPresenter implements Initializable {
@FXML
TableColumn tbcNombre, tbcApellidos, tbcFecha, tbcAsistencia;
@FXML
TableView tbvAlumnos;
private FilteredList<Alumno> datosFiltrados;
private SortedList<Alumno> listaOrdenada;
@Override
public void initialize(URL url, ResourceBundle rb) {
//Link columns with properties
tbcNombre.setCellValueFactory(new PropertyValueFactory<>("nombre"));
tbcApellidos.setCellValueFactory(new PropertyValueFactory<>("apellidos"));
//Assign data to table
datosFiltrados = new FilteredList<>(datamodel.getAlumnos());
//This is to make table searchable
listaOrdenada = new SortedList<>(datosFiltrados);
listaOrdenada.comparatorProperty().bind(tbvAlumnos.comparatorProperty());
tbvAlumnos.setItems(listaOrdenada);
//This show button but item always is null
tbcAsistencia.setCellFactory(new Callback<TableColumn<Alumno, Alumno>, TableCell<Alumno, Alumno>>() {
@Override
public TableCell<Alumno, Alumno> call(TableColumn<Alumno, Alumno> colAsistencia) {
return new TableCell<Alumno, Alumno>() {
@Override
protected void updateItem(Alumno item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
final Button btnAsistencia = new Button("Asistencia");
btnAsistencia.setOnAction((ActionEvent event) -> {
try {
//Where I clic on button, asistencia is not save because item is null
asistenciadao.guardar1(new Asistencia(new AsistenciaId(item, new Date())));
}
catch (ConstraintViolationException e) {
LOG.severe(e.toString());
new Dialogos().mostrarAdvertencia(null, "No se puede registrar más de una asistencia por día y alumno.", "");
}
catch (Exception e) {
LOG.severe(e.toString());
new Dialogos().mostrarExcepcion(null, e);
}
});
setGraphic(btnAsistencia);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
};
}
});
}
}