As popular jtable with xml file titles

Asked

Viewed 80 times

1

Hello, I have a cruel doubt about how to do something, I need to popular a Jtable with a list of xml files that are in a certain folder, but I have no idea how to do it or how to search for something. As popular a Jtable I already know but how to fetch the title of these files ( that are not in a BD ) I have no idea how to do, some light?

  • What you’ve tried to do?

  • So I don’t really know how to do it, what I know for example is to take data from the database and popular to Jtable, but in case now the XML files are in a folder on the server.

  • It would be interesting to add an attempt, otherwise the question falls too wide.

1 answer

1


To read the files return a list you can use the following method:

private List<String> lerNomesArquivo(String caminho) {
  File pasta = new File(caminho);
  List<String> nomes = new ArrayList<>();

  for (String nome : pasta.list()) {
    if (nome.endsWith(".xml")) {
      nomes.add(nome);
    }
  }

  return nomes;
}

To use the names for popular:

  private void popularTabela() {
    List<String> arquivos = this.lerNomesArquivo("C:/Users/Meu Usuário/Minha pasta");
    DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();

    int rowCount = modelo.getRowCount();

    // Remove linhas atuais
    for (int i = rowCount - 1; i >= 0; i--) {
      modelo.removeRow(i);
    }

    for (String arquivo : arquivos) {
      String[] linha = {arquivo};

      modelo.addRow(linha);
    }
  }

Browser other questions tagged

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