0

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);
                    }
                }
            };
        }
    });
  }
}
Marcos
  • 4,827
  • 3
  • 20
  • 28

2 Answers2

0

You need to set a cellValueFactory on your table column:

tbcAsistencia.setCellValueFactory(cellData -> new ReadOnlyObjectWrapper<Alumno>(cellData.getValue()));
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thanks @James_D, but I use your answer without using lambda expresion because I get an error on call to `.getValue()` so I fix with my posted solution that is similar. – Marcos Dec 23 '14 at 22:12
0

Similar aproach to @James_D answer:

            tbcAsistencia.setCellValueFactory(new Callback<CellDataFeatures<Alumno, Alumno>, ObservableValue<Alumno>>() {
                @Override
                public ObservableValue<Alumno> call(CellDataFeatures<Alumno, Alumno> features) {
                    return new ReadOnlyObjectWrapper(features.getValue());
                }
            });
Marcos
  • 4,827
  • 3
  • 20
  • 28