How popular jtable with xml file titles [closed]

1

Hello, I have a cruel question of how to do something, I need 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 . How do I add a JTable to a JTable? How do I get a JTable from a JTable?     

asked by anonymous 11.11.2016 / 17:59

1 answer

1

To read the files returning 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);
    }
  }
    
11.11.2016 / 18:37