Atualizar JProgressBar enquanto copiar arquivos para outro diretorio

2 respostas
V

Galera, é o seguinte, tou fazendo um programinha que enquanto eu copiava meu arquivos para outro diretorio ele também atualizava o JProgressBar, isso eu conseguir, mas o frame trava na hora da execução e só aparece quando está carregado por completo, como faço para atualizar junto ? Com Thread ? se for, como seria ela ?

Abraços

2 Respostas

V

Ops, esqueci o código.

package View;

import java.awt.BorderLayout;

public class frmSinc extends JFrame {

	private JPanel contentPane;
	private int status, cont;
	private JProgressBar progressBar;
	


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frmSinc frame = new frmSinc();					
				
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws IOException 
	 */
	public frmSinc() throws IOException {
		setTitle("Atualizar programas");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 402, 222);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		progressBar = new JProgressBar();
		progressBar.setBounds(31, 92, 338, 16);
		contentPane.add(progressBar);
		progressBar.setIndeterminate(false);
		setVisible(true);
		Copiar("E:\\Documents and Settings\\Valério\\Desktop\\IBExpert 2007\\", "E:\\Documents and Settings\\Valério\\Desktop\\uou\\");
	}
	
	public void Copiar(String origem, String destino) throws IOException {
		File fileOrigem = new File(origem);
		File fileDestino = new File(destino);
		
		if (!(fileDestino.exists())) {
			fileDestino.mkdir();	// se o diretório não existir, ele cria.		
		}
		
		cont = fileOrigem.listFiles().length;
		progressBar.setMaximum(cont);
		progressBar.setValue(0);
		for (File f: fileOrigem.listFiles()) {
			if (f.isFile()) {
				progressBar.setValue(++cont);
				
				FileChannel oriChannel = new FileInputStream(origem + f.getName()).getChannel();
				// Cria channel no destino
				FileChannel destChannel = new FileOutputStream(destino + f.getName()).getChannel();
				
				// Copia conteúdo da origem no destino
				destChannel.transferFrom(oriChannel, 0, oriChannel.size());

				// Fecha channels
				oriChannel.close();
				destChannel.close();
			}
		}		
		
	}
}
fernandopaiva

Opa, use SwingWorker…

veja: http://www.guj.com.br/java/245786-jprogressbar-com-swingworker-

t+ e boa sorte

Criado 4 de outubro de 2011
Ultima resposta 5 de out. de 2011
Respostas 2
Participantes 2