2010-08-04 2 views
0

** 안녕하세요, Java에서 아카이버를 만들려고합니다. 즉, 나는 끊임없이 스트림에서 읽고 쓰고 있습니다. JProgressBar를 업데이트하여 필자가 작성한 내용을 보여줄 수 있어야합니다. 내 코드는 현재 아카이브의 각 항목 이후 진행률 막대를 업데이트합니다. 내 변수와 모든 것은 코드가 화면에 표시되지 않는 것을 제외하고는 업데이트된다고 말합니다. 아카이브에 진입 한 후에는 진행률 표시 줄과 텍스트가 포함 된 JPanel에서 revalidate() 메서드를 호출하지만 화면에는 아무 것도 표시되지 않습니다. 아카이브를 쓰는 출력 스트림이 완전히 쓰여질 때까지 화면이 업데이트되지 않습니다. 그런 다음 진행률 텍스트에 "24 개의 파일 중 24 개가 기록되었습니다"라는 전체 진행률 막대가 표시됩니다.코드가 완료 될 때까지 JComponent.revalidate()가 업데이트되지 않습니다.

누구든지 내 코드가 실행되기 전에 그래픽을 업데이트 할 수있는 방법을 알려 줄 수 있습니까?

WritingBox.java :

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") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
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(); 
}// </editor-fold> 

// 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 

} 

Progress.java

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

TarArchive.java : doSave 방법

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

수있는 사람 여기

고맙습니다

내 코드입니다 도와주세요? 각 항목 다음에 revalidate()를 호출하더라도 코드가 중지되고 기다리기 시작할 때만 업데이트됩니다. **

답변

1

AWT Event Dispatch Thread (EDT)에서 파일 I/O를하고있는 것처럼 들립니다. 이것은 모든 페인트 또는 이벤트 처리를 차단합니다. 다른 스레드에서 I/O를 실행하고 java.awt.EventQueue.invokeLater을 통해 GUI를 업데이트하십시오.

+0

어떻게하면됩니까? –

+0

@ user411316 : 예를 들어, "객체로 계속"을 참조하십시오. http://en.wikipedia.org/wiki/Continuation_passing_style#Continuations_as_objects – trashgod

관련 문제