TableView does not display data with JavaFx

0

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>
    
asked by anonymous 24.05.2016 / 15:25

1 answer

0

Within your List load () method

Have you ever tried to populate the columns of the tableview to only after returning it?

For example:

Column "NAME" (clName)

clName.setCellValueFactory (data-> data.getValue (). getName ());

And in the class of the respective entity, for example:

public class Person {

private end StringProperty Name;

public Person (String Name) {         this.firstName = new SimpleStringProperty (Name);

}

public String getName() {
    return Name.get();
}

public void setName(String Name) {
    this.Name.set(Name);
}

public StringProperty NameProperty() {
    return Name;
}

}

This way your method would be:

 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);
            }

           **//populando a coluna Name da TableView
           clName.setCellValueFactory(data->data.getValue().getName());**

           return tags;
       }

   }
    
23.09.2016 / 22:29