Note that there are some explanatory texts on larger screens.

plurals
  1. POJComponent.revalidate() doesn't update until code finishes
    primarykey
    data
    text
    <p>**Hello, I'm trying to create an archiver in java. This means that I am constantly reading and writing from streams. I want to be able to update a JProgressBar to show how much I have written. My code currently updates the progress bar after each entry in the archive. My variables and everything say that the code is updated, except its not showing on the screen. After each entry into the archive I call the revalidate() method on the JPanel that contains the progress bar and text, but nothing happens on the screen. The screen doesn't update until the output stream that is writing the archive has completely finished writing. Then, I see the full progress bar with "24 out of 24 files written" in the progress text.</p> <p>Can anyone tell me how I can get the graphics to update before my code finished executing? Thanx</p> <p>Here is my code:</p> <p>WritingBox.java: </p> <pre><code>package org.apache.commons.compress; import javax.swing.JDialog; public class WritingBox extends javax.swing.JDialog { /** Creates new form WritingBox */ public WritingBox(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); theprogressbar.setMaximum(1); theprogressbar.setMinimum(0); this.setLocation(parent.getLocationOnScreen().x+(parent.getWidth()-this.getWidth())/2, parent.getLocationOnScreen().y+(parent.getHeight()-this.getHeight())/2); this.setVisible(true); } public void progressListener(Progress p) { writtenxoutofx.setText("Written "+Integer.toString(p.doneSoFar)+" out of "+Integer.toString(p.total)+" files to the Archive"); theprogressbar.setValue(p.doneSoFar/p.total); if (p.doneSoFar == p.total) this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); thecontainer.revalidate(); }; /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt; private void initComponents() { thecontainer = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); writtenxoutofx = new javax.swing.JLabel(); theprogressbar = new javax.swing.JProgressBar(); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); setName("Form"); // NOI18N thecontainer.setName("thecontainer"); // NOI18N org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(targzipmanager.TarGzipManagerApp.class).getContext().getResourceMap(WritingBox.class); jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N jLabel1.setName("jLabel1"); // NOI18N writtenxoutofx.setText(resourceMap.getString("writtenxoutofx.text")); // NOI18N writtenxoutofx.setName("writtenxoutofx"); // NOI18N theprogressbar.setName("theprogressbar"); // NOI18N javax.swing.GroupLayout thecontainerLayout = new javax.swing.GroupLayout(thecontainer); thecontainer.setLayout(thecontainerLayout); thecontainerLayout.setHorizontalGroup( thecontainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(thecontainerLayout.createSequentialGroup() .addContainerGap() .addGroup(thecontainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(theprogressbar, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE) .addComponent(jLabel1) .addComponent(writtenxoutofx)) .addContainerGap()) ); thecontainerLayout.setVerticalGroup( thecontainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(thecontainerLayout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(writtenxoutofx) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(theprogressbar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(thecontainer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(thecontainer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// &lt;/editor-fold&gt; // Variables declaration - do not modify private javax.swing.JLabel jLabel1; private javax.swing.JPanel thecontainer; private javax.swing.JProgressBar theprogressbar; private javax.swing.JLabel writtenxoutofx; // End of variables declaration } </code></pre> <p>Progress.java </p> <pre><code>package org.apache.commons.compress; public class Progress { //This is basically a Struct public int total; public int doneSoFar; private Progress (){ } public Progress(int d, int t){ total = t; doneSoFar = t; } } </code></pre> <p>TarArchive.java: doSave method </p> <pre><code>public void doSave(OutputStream output) throws ArchiveException { // Stream initializing //BufferedInputStream origin = null; //out.setMethod(ZipOutputStream.DEFLATED); //byte data[] = new byte[BUFFER]; // get a list of files from current directory // less than one file leads to an exception Iterator iterator = this.getEntryIterator(); if(!iterator.hasNext()) { throw new ArchiveException("There must be at least one file to be pack."); } JFrame jf = targzipmanager.TarGzipManagerApp.getApplication().getMainFrame(); WritingBox wb = new WritingBox(jf, false); TarOutputStream out = null; int x = 0; try { out = new TarOutputStream(new BufferedOutputStream(output)); while(iterator.hasNext()) { x++; ArchiveEntry archiveEntry = (ArchiveEntry)iterator.next(); InputStream fInputStream = archiveEntry.getStream(); TarEntry entry = new TarEntry(archiveEntry.getName()); entry.setModTime( 0 ); entry.setSize( fInputStream.available() ); entry.setUserID( 0 ); entry.setGroupID( 0 ); entry.setUserName( "avalon" ); entry.setGroupName( "excalibur" ); entry.setMode( 0100000 ); out.putNextEntry( entry ); out.copyEntryContents( fInputStream ); out.closeEntry(); wb.progressListener(new Progress(x, entries.size())); } } catch (IOException e) { throw new ArchiveException("Creation of this archive failed cause of IOExceptions.", e); } finally { try { out.close(); } catch (IOException e1) { throw new ArchiveException("Creation of this archive failed cause of IOExceptions.", e1); } } } </code></pre> <p>Can anyone help me? Even though I'm calling revalidate() after each entry, it only updates when the code stops and it starts waiting. **</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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