Code Error for Popular Jlist with File Names of a Folder

Asked

Viewed 161 times

0

I cannot pass a method to get the name of the files in a folder.

I have the code below:

inicializar.addWindowListener(new WindowListener(){
       @Override
       public void windowOpened(WindowEvent arg0){

            DefaultListModel dlm = new DefaultListModel();
            JList lstvRel = new JList();
            lstvRel.setModel(dlm);
            ListarArquivos l = new ListarArquivos();
            dlm = l.listar(); //Erro nesta linha!!
            for(int i = 0; i < l.listar().size(); i++){
                lstvRel.getModel().getElementAt(i).toString();                    
            }    

        }

And the class:

public class ListarArquivos {


    public void listar() {

        File dirArquivos = new File("C:\\Users\\lbell\\Desktop\\Turbo - teste");

        File[] Arquivos = dirArquivos.listFiles((File b) -> b.getName().endsWith(".xls") ||
                    b.getName().endsWith(".xlsx") ||
                    b.getName().endsWith(".xlsm") ||
                    b.getName().endsWith(".xlsb") ||
                    b.getName().endsWith(".ppt"));
    }

}
  • The method returns void, what’s waiting for him to return?

  • As Renan said, the list method is void, there is no return, and you are trying to assign a return that does not exist to a variable.

  • Sorry to be unaware, I’m new to java and I’m creating a report generator program. In the case then how could I create a return to my void generate an array. The intention is to popular a Jlist when initializing the program.

1 answer

1

If your goal is to "popular a Jlist when starting the program.", the implementation is a little different.

There are several ways to do this, using the methods you have already defined, to do the following:

In the class of your object 'initialize', you must create and add elements in your Defaultlistmodel dlm in the constructor and put in your Jlist. A small example:

class Inicializar {

   JList lstRel;

   Inicializar(String dir) {

     // Cria a DefaultListModel
     DefaultListModel<String> dlm = new DefaultListModel<String>();

     // Cria a array de arquivos do diretorio
     File[] arquivos = (new File(dir)).listFiles((File b) -> b.getName().endsWith(".xls") ||
                b.getName().endsWith(".xlsx") ||
                b.getName().endsWith(".xlsm") ||
                b.getName().endsWith(".xlsb") ||
                b.getName().endsWith(".ppt"));

     // Adiciona os arquivos na DefaultListModel
     for(int i=0; i < arquivos.length; i++)
       addElement(arquivos[i].getName());

     // Coloca sua dlm em uma JList
     JList lstvRel = new JList(dlm);

   } 

   public JList getList() {

     return lstvRel;

  }

}
  • The example I could understand, but as I said earlier, I’m new to Java. When I tried to use something similar, I had trouble declaring in the main of my main class a call to this class. Another question I have is this: in my project’s Windowbuilder, I entered the Jlist I named as lstvRel, how can I make a call to it to be populated when opening the program? If you have any tutorial link thank you, because I am studying to learn.

  • To start is simple: Initialize init = new Initialize("documents/files/"); I forgot to tell you, but the constructor is responsible for instantiating the class that was defined (see more: http://www.devmedia.com.br/constructes-em-java/28618). That is, whenever you start the program you will instantiate the Initialize class and thus call your constructor (popular your list). So you can take Jlist like this: Jlist lstvRel = init.getList();

Browser other questions tagged

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