Note that there are some explanatory texts on larger screens.

plurals
  1. POJava file a progress bar
    text
    copied!<p>I'm new to Java. I want to write a program that makes arcive of file and i want to use progress bar to show progress of zipping There is my code, but it duosen't work shows only 100% when work is done.</p> <pre><code> getContentPane().add(progressBar); progressBar.setBorder(new CompoundBorder(null, null)); springLayout.putConstraint(SpringLayout.EAST, progressBar, 0, SpringLayout.EAST, messageLabel); springLayout.putConstraint(SpringLayout.WEST, progressBar, 0, SpringLayout.WEST, messageLabel); springLayout.putConstraint(SpringLayout.SOUTH, progressBar, 165, SpringLayout.NORTH, getContentPane()); springLayout.putConstraint(SpringLayout.NORTH, progressBar, 5, SpringLayout.SOUTH, uploadLocalButton); progressBar.setValue(0); progressBar.setMaximum(100); progressBar.setMinimum(0); progressBar.setStringPainted(true); ... public void doZip(String filename,String outFilename) { try { byte[] dataBytes = new byte[1024]; File file = new File(outFilename); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); ZipOutputStream zos = new ZipOutputStream(fos); File fil = new File(filename); //"Users/you/image.jpg" long length= fil.length(); System.out.println(length); zos.putNextEntry(new ZipEntry(fil.getName()));//image.jpg FileInputStream in = new FileInputStream(filename); int current=0; int len; while ((len = in.read(dataBytes)) &gt; 0) { current += len; messageLabel.setText(Integer.toString(current)); System.out.println(current); final double progres=(((double)current/(double)length)*100); progressBar.setValue((int)progres); zos.write(dataBytes, 0, dataBytes.length); } zos.closeEntry(); zos.close(); in.close(); }catch (IOException e) { JOptionPane.showMessageDialog(null, "Access denied. File or patch not exist or in currently in use.","Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); }} </code></pre> <p>Maybe someone knows where is the problem?</p> <hr> <p>I call <strong>doZip</strong> by pressing the botton. I tried this </p> <pre><code> while ((len = in.read(dataBytes)) &gt; 0) { current += len; zos.write(dataBytes, 0, dataBytes.length); final double progres=(((double)current/(double)length)*100); try{ SwingUtilities.invokeLater(new Runnable(){ public void run(){ progressBar.setValue((int)progres); } }); java.lang.Thread.sleep(100); }catch(InterruptedException e){;} </code></pre> <p>but it also doesent work, only makes process of zip opperation much more longer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload