JList - does not update when logging in again

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();
            }
        }

    });

The new1 is a button to create a new file. This code excerpt works, the problem is when I close the frame and reopen it it does not assume that it has the new file I created. How do I update the frame?

    
asked by anonymous 12.10.2016 / 16:12

2 answers

2

What you can do is go to the directory where you have the files and go to 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: Do not forget that if you want to load when you start JFrame then this method has to be called as soon as you load JFrame.

    
12.10.2016 / 17:40
0

My own question did not contain errors, I just accepted files in the program that ended in .rtf and was creating a ".txt". In this case I created a folder inside the project called file, so I go into that folder in the instance "directory.execution".

If you want ".txt" files f.getName (). endsWith (". rtf");

I leave here the part that I add pc files 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());
    }
    
14.10.2016 / 00:33