I have the following problem I have a TableView in JavaFX has 3 columns of which the last column is a button. The situation is that the button must be un-enabled for the first 5 records of the table, but if the table has a scroll long enough so that these lines disappear when they reappear they are rehabilitated.
If I add the following code, the problem does not happen but the events of the buttons are also disabled
tblCamposMaestro.addEventFilter( Event.ANY, scrollEvent -> {
tblCamposMaestro.refresh();
});
Try this code but it does not work for some reason beyond my comprehension:
tblCamposMaestro.addEventFilter( ScrollEvent.ANY, scrollEvent -> {
tblCamposMaestro.refresh();
});
The table is filled in the following way:
introducir el código aquí
private void crearColumnas(){
columnaNombre = new TableColumn("Nombre");
columnaTipo = new TableColumn("Tipo");
columnaEliminar = new TableColumn("Eliminar");
columnaNombre.prefWidthProperty().bind(tabla.widthProperty().divide(3));
columnaTipo.prefWidthProperty().bind(tabla.widthProperty().divide(3));
columnaEliminar.prefWidthProperty().bind(tabla.widthProperty().divide(3));
columnaNombre.setCellFactory(new Callback<TableColumn<Object, String>, TableCell<Object,String>>(){
@Override
public TableCell<Object, String> call(TableColumn<Object, String> param){
TableCell tc = new TableCell<Object, String>(){
@Override
protected void updateItem(String item, boolean empty){
super.updateItem(item, empty);
if (getIndex() >= 0 && getIndex() <campos.size()) {
setAlignment(Pos.CENTER);
setText(""+ campos.get(getIndex()).getNombre());
}
}
};
return tc;
}
});
columnaTipo.setCellFactory(new Callback<TableColumn<CampoBD, String>, TableCell<CampoBD,String>>(){
@Override
public TableCell<CampoBD, String> call(TableColumn<CampoBD, String> param){
TableCell tc = new TableCell<Object, String>(){
@Override
protected void updateItem(String item, boolean empty){
super.updateItem(item, empty);
if (getIndex() >= 0 && getIndex() <campos.size()) {
setAlignment(Pos.CENTER);
setText(""+ campos.get(getIndex()).getTipo());
}
}
};
return tc;
}
});
columnaEliminar.setCellValueFactory( new PropertyValueFactory<>( "DUMMY" ) );
Callback<TableColumn<CampoBD, String>, TableCell<CampoBD, String>> cellFactory = //
new Callback<TableColumn<CampoBD, String>, TableCell<CampoBD, String>>()
{
@Override
public TableCell call( final TableColumn<CampoBD, String> param )
{
final TableCell<CampoBD, String> cell = new TableCell<CampoBD, String>()
{
final Button btn = new Button( "Eliminar" );
@Override
public void updateItem( String item, boolean empty )
{
super.updateItem( item, empty );
if ( empty )
{
}
else
{
if (getIndex()< 5) {
setGraphic( btn );
btn.setDisable(true);
setAlignment(Pos.CENTER);
setText( null );
}
else
{
btn.setOnAction( ( ActionEvent event ) ->
{
CampoBD person = getTableView().getItems().get( getIndex() );
System.out.println( person.getNombre()+ " " + person.getTipo());
} );
setGraphic( btn );
setAlignment(Pos.CENTER);
setText( null );
}
}
}
};
return cell;
}
};
tabla.addEventFilter(ScrollEvent.ANY, scrollEvent -> {
tabla.refresh();
// close text box
tabla.edit(-1, null);
});
columnaEliminar.setCellFactory( cellFactory );
}
public void actualizar(List<CampoBD> alumnos)
{
this.campos = FXCollections.observableArrayList(alumnos);
tabla.setItems(this.campos);
tabla.addEventFilter(ScrollEvent.ANY, scrollEvent -> {
tabla.refresh();
});
}
private void agregarColumnas() {
tabla.getColumns().clear();
tabla.getColumns().addAll(columnaNombre,columnaTipo,columnaEliminar);
tabla.addEventFilter(ScrollEvent.ANY, scrollEvent -> {
tabla.refresh();
});
}