Esta acontecendo um problema ao criar minha tableView, as colunas e os dados são criados normalmente, o problema é que sempre fica com uma coluna a mais na tabela(a coluna fica sem titulo e sem dados), porém mesmo assim ela é criada e não sei o porque.
Fiz uma classe onde tenho um método que constroe a tableView pra mim, somente passo um ObservableList para o método
package application.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;
public class TableViewFactory {
@SuppressWarnings({ "unchecked", "rawtypes","unused" })
public TableView criarTabela(ObservableList objetos) {
List<String> fields = new ArrayList<>();
List<TableColumn> tableColumns = new ArrayList<>();
List<Object> objetosReflection = new ArrayList<Object>();
TableView table = new TableView(objetos);
for (Field f : objetos.get(0).getClass().getDeclaredFields()) {
fields.add(f.getName().substring(0, 1).toUpperCase() + f.getName().substring(1));
}
for (String coluna : fields) {
TableColumn tableColumn = new TableColumn(coluna);
tableColumn.setCellValueFactory(new PropertyValueFactory(coluna));
tableColumns.add(tableColumn);
}
TableColumn actionColEditar = new TableColumn( "Editar" );
actionColEditar.setCellValueFactory( new PropertyValueFactory<>( "Button" ) );
Callback<TableColumn<Object,String>, TableCell<Object,String>> cellFactoryEditar =
new Callback<TableColumn<Object,String>, TableCell<Object,String>>()
{
@Override
public TableCell<Object,String> call( final TableColumn<Object,String> param )
{
final TableCell<Object,String> cell = new TableCell<Object,String>()
{
final Button btn = new Button( "Editar" );
public void updateItem( String item, boolean empty )
{
super.updateItem( item, empty );
if ( empty )
{
setGraphic( null );
setText( null );
}
else
{
btn.setOnAction( ( ActionEvent event ) ->
{
Object objeto = getTableView().getItems().get(getIndex());
} );
setGraphic( btn );
setText( null );
}
}
};
return cell;
}
};
TableColumn actionColExcluir = new TableColumn( "Excluir" );
actionColExcluir.setCellValueFactory( new PropertyValueFactory<>( "Button" ) );
Callback<TableColumn<Object,String>, TableCell<Object,String>> cellFactoryExcluir =
new Callback<TableColumn<Object,String>, TableCell<Object,String>>()
{
@Override
public TableCell<Object,String> call( final TableColumn<Object,String> param )
{
final TableCell<Object,String> cell = new TableCell<Object,String>()
{
final Button btn = new Button( "Excluir" );
public void updateItem( String item, boolean empty )
{
super.updateItem( item, empty );
if ( empty )
{
setGraphic( null );
setText( null );
}
else
{
btn.setOnAction( ( ActionEvent event ) ->
{
Object objeto = getTableView().getItems().get(getIndex());
} );
setGraphic( btn );
setText( null );
}
}
};
return cell;
}
};
actionColEditar.setCellFactory( cellFactoryEditar );
actionColExcluir.setCellFactory(cellFactoryExcluir);
table.setItems(objetos);
for(TableColumn tableColumn : tableColumns){
table.getColumns().add(tableColumn);
}
table.getColumns().add(actionColEditar);
table.getColumns().add(actionColExcluir);
return table;
}
}