更新JTextArea以显示进度

时间:2017-01-09 14:33:34

标签: java user-interface swingworker

我正在申请压缩和删除文件。到目前为止,我已经设法实现了SwingWorker,但它没有显示和更新TextArea。结果仅显示在控制台中。有人能告诉我一个例子我做错了什么或我错过了什么?我制作的代码如下所示:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.*;
import javax.swing.*;
import java.util.List;

@SuppressWarnings("serial")
public class zipfiles2 extends JFrame {
static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
private File datei, parent;
private String name;

class Task extends SwingWorker<String, String>
{
  String status;
  JTextArea statusprocess;

  public Task(JTextArea statusprocess)
  {
      this.statusprocess = statusprocess;
  }
  private String addToZip(String path, String srcFile, ZipOutputStream zipOut) throws IOException
  {        
    File file = new File(srcFile);
    String filePath = "".equals(path) ? file.getName() : path + "/" + file.getName();
    if (file.isDirectory()) 
    {
      for (String fileName : file.list()) 
      {
        status = "Folder: "+srcFile + "/" +" is being added to the zip file";
        addToZip(filePath, srcFile + "/" + fileName, zipOut);
      }
    } 
    else 
    {
        if (new File(filePath).canRead())
        {
            status = "File "+filePath+"is being zipped";
        }
        else
        {
            status = "File: "+filePath+" is being zipped";
            zipOut.putNextEntry(new ZipEntry(filePath));

            FileInputStream in = null;
            try {
                    in = new FileInputStream(srcFile);
            } catch (FileNotFoundException e) {
                new File(srcFile).delete();
            }

            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int len;
            while ((len = in.read(buffer)) != -1) {
                zipOut.write(buffer, 0, len);
            }
            in.close();
        }
    }
    return status;
  }

public String zipFile(String fileToZip, String zipFile, boolean excludeContainingFolder) throws IOException
  {        
      ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));    
      File srcFile = new File(fileToZip);

      if(excludeContainingFolder && srcFile.isDirectory()) 
      {
          for(String fileName : srcFile.list()) 
          {
                addToZip("", fileToZip + "/" + fileName, zipOut);
          }
       } 
       else 
       {
           addToZip("", fileToZip, zipOut);
       }
       zipOut.flush();
       zipOut.close();
       status = "The zip file " + zipFile + " was created successfully";
       return status;
  }
  public String deleteDirectory(File f) throws IOException  {
        if (f.isDirectory()) {
            for (File c: f.listFiles()) {
                status= "File: "+c.getName()+" is being deleted"; 
                deleteDirectory(c);
            }
            f.delete();
        }
        else 
        {
            f.delete();
        }
        return status;

  }
@Override
protected String doInBackground() throws Exception {
    zipFile(datei.getAbsolutePath(), parent.getAbsolutePath()+"/"+name, true);
    publish(status);
    deleteDirectory(datei);
    publish(status);
    return status;
}

@Override
protected void process(List<String> chunks)
{
    for (String status : chunks) {
        statusprocess.append(status);
    }
}
 }
 public zipfiles2() {

  JFileChooser browser = new JFileChooser();
  browser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  JPanel panel = new JPanel();
  final JTextArea showprogress = new JTextArea();

  ActionListener fileValidation = new ActionListener()
  {
    public void actionPerformed(ActionEvent actionEvent)
    {
        JFileChooser browser2 = (JFileChooser) actionEvent.getSource();
        String command = actionEvent.getActionCommand();

        if (command.equals(JFileChooser.APPROVE_SELECTION))
        {
            datei = browser2.getSelectedFile();
            parent = browser2.getCurrentDirectory();
            File[] dio = parent.listFiles();

            if (datei.isFile()) { name = datei.getName().substring(0, datei.getName().lastIndexOf("."))+".zip"; }
            else { name = datei.getName()+".zip"; }

            for (File dateien: dio)
            {   
                if (dateien.isFile() && dateien.getName().endsWith(".zip"))
                {
                   if (name.equals(dateien.getName())) 
                   {
                       JOptionPane.showMessageDialog(null,"Eine Zip-Datei mit der Name der ausgewählten Datei existiert bereits im Ordner", "Validierung", JOptionPane.WARNING_MESSAGE);
                       new zipfiles2().setVisible(true);
                   }
                }
            }
            Task task = new Task(showprogress);
            task.execute();
         } 
         else
         {
            System.exit(0);  
         }
      }

  };

  browser.addActionListener(fileValidation);


  JProgressBar progessbar = new JProgressBar();
  progressbar.setIndeterminate(true);
  panel.add(progressbar);
  panel.add(showprogress);

  Dimension ScS = Toolkit.getDefaultToolkit().getScreenSize();
  this.setTitle("Zip Tool");
  this.setSize(200, 90);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setLocation(ScS.width/2 - this.getSize().width/2, ScS.height/2 - this.getSize().height/2);

  browser.showOpenDialog(this);
  this.add(panel, BorderLayout.PAGE_START);
}
 public static void main(String[] args) {
     java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try 
                {
                    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");     
                } 
                catch (Exception ex) 
                {
                    ex.printStackTrace();
                }
                new zipfiles2().setVisible(true);
            }
        });
  }
}

编辑:上面的代码是新版本,并提供了更正。但是,它还没有显示我的GUI。我还缺少什么吗?

第二次编辑:我的GUI已显示,但我的textarea未更新。我应该如何将其他方法的状态传递给doInBackground方法才能发布?也许是ArrayList类型String?

1 个答案:

答案 0 :(得分:1)

您未正确覆盖process方法,签名如下:

protected void process(List<V> chunks)
在你的情况下,它将是:

protected void process(List<String> chunks)

使用@Override注释可以防止出现此问题。

然后,你会这样使用它:

     @Override
     protected void process(List<String> chunks) {
         for (String status : chunks) {
             statusprocess.append(status);
         }
     }