Tableview does not display data with Javafx

Asked

Viewed 443 times

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/

  • 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.

1 answer

0

Within your List load method()

tried to popular tableview columns only to return it later?

For example:

Column "NAME" (code)

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

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

public class Person {

private final 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;
       }

   }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.