Visualizzazione dei risultati da 1 a 10 su 12

Hybrid View

  1. #1
    ciao andbin!


    ho provato a cambiare usando SwingWorker.
    questo pare funzionare:
    codice:
                    FileChooserOpen fco = new FileChooserOpen();
                    fco.showOpenDialog(null);
                    File dir = fco.getDir();
                    SwingWorker worker = new SwingWorker<Void, Void>() {
                        @Override
                        protected Void doInBackground() {
                            try {
                                north.getBtnStop().setEnabled(true);
                                bar.setIndeterminate(true);
                                bar.setString("Loading...");
                                Scan utils = new Scan();
                                ArrayList<Record> list = utils.scanDir(dir.toPath());
                                countFile = list.size();
                                for (int i = 0; i < list.size(); i++) {
                                    String n = list.get(i).getNome();
                                    String t = list.get(i).getTipo();
                                    String p = list.get(i).getPath();
                                    Object[] row = {n, t, p};
                                    model.addRow(row);
                                    try {
                                        Thread.sleep(200);
                                    } catch (InterruptedException ex) {
                                        JOptionPane.showMessageDialog(null, ex.getMessage());
                                    }
                                }
                            } catch (IOException ex) {
                                JOptionPane.showMessageDialog(null, ex.getMessage());
                            }
                            return null;
                        }
    
                        @Override
                        public void done() {
                            north.getLabelCount().setText("No. file " + countFile);
                            north.getBtnStop().setEnabled(false);
                            resizeColumnWidth();
                            bar.setIndeterminate(false);
                            bar.setString("");
                        }
                    };
                    worker.execute();
    ma a prescindere dal fatto che funzioni, può andare come soluzione?

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da fermat Visualizza il messaggio
    ho provato a cambiare usando SwingWorker.
    Il succo, purtroppo, NON cambia. doInBackground è eseguito in un thread che non è il EDT. Quindi:

    north.getBtnStop().setEnabled(true);
    bar.setIndeterminate(true);
    bar.setString("Loading...");
    model.addRow(row);
    JOptionPane.showMessageDialog(null, ex.getMessage());

    NON li dovresti fare lì così.
    Andrea, Senior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.