Hello, I am building a tableView with a tableview loaded with information from the database and FXML. I noticed that the tableview receives the values from my dao because it displays exactly the number of rows from the query when doing a foreach and the data is ok, but the tableview appears without the rows populated. Source: controller
public class TableViewController {
private TagsDAO dao;
private List<Tags> tagsList;
//private DateTimeFormatter fomatador = DateTimeFormatter.ofPattern("dd/MM/yyyy");
@FXML
private TableView<Tags> tbv1;
@FXML
private void initialize(){
//obtém o objeto DAO
dao = DAOFactory.getTagsDAO();
// Adiciona um listener para ser notificado quando o usuário seleciona um item na tabela.
// Dessa forma é possível definir os bindings corretamente.
loadData();
}
private void loadData(){
try {
tagsList = dao.load();
ObservableList<Tags> list = FXCollections.observableArrayList(tagsList);
tbv1.setItems(list);
} catch (Exception e) {
e.printStackTrace();
}
}
}
DAO Method
public List<Tags> load() {
try (Connection conn = ConnectionFactory.openConnection()) {
sql = "SELECT dt.Tag, dt.Id FROM DadoTag dt ORDER BY dt.Id ASC";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
// executa a busca
try (ResultSet rs = stmt.executeQuery()) {
List<Tags> tags = new ArrayList<>();
// para cada registro encontrado na tabela, cria um objeto
// Tag e coloca na lista
while (rs.next()) {
Tags tagsModel = new Tags();
tagsModel.setDescTag(rs.getString("Tag"));
tagsModel.setId(rs.getInt("Id"));
tags.add(tagsModel);
}
return tags;
}
}
} catch (SQLException e) {
alert = new Alert(AlertType.ERROR);
alert.setTitle("...");
alert.setHeaderText("Erro");
alert.setContentText(e.toString());
alert.showAndWait();
throw new DAOException(e);
}
}
FXML:
<TableView fx:id="tbv1" layoutX="8.0" layoutY="9.0" prefHeight="215.0" prefWidth="316.0">
<columns>
<TableColumn text="TAG" prefWidth="100">
<cellValueFactory property="TAG"/>
</TableColumn>
</columns>
</TableView>