Jlist - does not update when I log in again

Asked

Viewed 79 times

1

private DefaultListModel model = new DefaultListModel<>();          //modelo da lista de ficheiros
private JList listaFicheiros = new JList<>(model);      

new1.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e){
            String nomeFicheiroNovo = JOptionPane.showInputDialog(frame, "Qual o nome do novo ficheiro?");

            //Criar um ficheiro novo e escrever para dentro dele

            File ficheiroNovo = new File(diretoriaExecucao + "/" + nomeFicheiroNovo + ".txt");   //dentro e a diretoria que queremos gravar ficheiro (em execucao)

            //escrever para o ficheiro
            try {
                PrintWriter printWriter = new PrintWriter(ficheiroNovo); 
                printWriter.println("Escreveu ficheiro?");                          //O que queremos escrever no ficheiro.
                model.addElement(ficheiroNovo.getName());                           //adicionar ficheiro à lista para aparecer frame

                printWriter.close();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
        }

    });

new1 is a button to create a new file. This snippet of code works, the problem is when I close the frame and open again it does not assume that it has the new file I created. How I update the frame?

  • 1

    But according to the above code, it will only be updated when the button is clicked. What you want is to run this without having to click the button?

  • I open the frame I have two files in jlist but then when I add the file with the new name it appears in the list, but when I close the frame and open it again I only see the 2 files that were already there

  • Can add a [mcve] so that it is possible to simulate the problem?

  • When I was on the computer I do this

2 answers

2


What you can do is go to the directory where you have the files and get the last modified file that I assume is the new file.

private File getLatestFilefromDir(String dirPath){
    File dir = new File(dirPath);
    File[] files = dir.listFiles();
    if (files == null || files.length == 0) {
        return null;
    }

    File lastModifiedFile = files[0];
    for (int i = 1; i < files.length; i++) {
        if (lastModifiedFile.lastModified() < files[i].lastModified()) {
            lastModifiedFile = files[i];
        }
    }
    return lastModifiedFile;
}

Here’s a link to what you need.

Note: don’t forget that if you want to load when you start Jframe then this method has to be called as soon as you load Jframe.

  • This method is also correct! Thank you

0

My own question contained no errors, I was only accepting files in the program that ended in ". rtf" and was creating a ". txt". In this case I created a folder inside the project that is called file, that is why I enter that folder in the instance "directorexecucao".

In case you want ". txt" meter f.getName(). endsWith(". rtf files");

I leave here the part that I add files from the pc to the list:

    private String diretoriaExecucao = System.getProperty("user.dir") + File.separator + "ficheiro";


    File[] files = new File(diretoriaExecucao).listFiles(new FileFilter() {

        @Override
        public boolean accept(File f) {
            return f.getName().endsWith(".rtf");        //so aceita ficheiros acabados em .rtf
        }
    });

    for(int i = 0; i < files.length; i++){          //como nao e array percorremos o tamanho por inteiro do array
        model.addElement(files[i].getName());
    }

Browser other questions tagged

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