Atualizar JProgressBar enquanto copiar arquivos para outro diretorio
2 respostas
V
Valeio_Bezerra
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 ?
packageView;importjava.awt.BorderLayout;publicclassfrmSincextendsJFrame{privateJPanelcontentPane;privateintstatus,cont;privateJProgressBarprogressBar;/** * Launch the application. */publicstaticvoidmain(String[]args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{frmSincframe=newfrmSinc();}catch(Exceptione){e.printStackTrace();}}});}/** * Create the frame. * @throws IOException */publicfrmSinc()throwsIOException{setTitle("Atualizar programas");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,402,222);contentPane=newJPanel();contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);contentPane.setLayout(null);progressBar=newJProgressBar();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\\");}publicvoidCopiar(Stringorigem,Stringdestino)throwsIOException{FilefileOrigem=newFile(origem);FilefileDestino=newFile(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(Filef:fileOrigem.listFiles()){if(f.isFile()){progressBar.setValue(++cont);FileChanneloriChannel=newFileInputStream(origem+f.getName()).getChannel();// Cria channel no destinoFileChanneldestChannel=newFileOutputStream(destino+f.getName()).getChannel();// Copia conteúdo da origem no destinodestChannel.transferFrom(oriChannel,0,oriChannel.size());// Fecha channelsoriChannel.close();destChannel.close();}}}}