0
Good morning, I am mounting a layout with tableView loaded with database information and FXML, while running realized that the tableview receives the values of my dao because it displays exactly the amount of lines coming from the query, when making a foreach and the data are ok, but the tableview appears without the lines filled in. 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>
See if this helps you: http://code.makery.ch/library/javafx-8-tutorial/pt/part2/
– DiegoAugusto
Good morning, thanks for the reply, I had already seen the tutorial and trying to do this way and still did not succeed. Thanks for the help.
– Ronaldo Lopes