Criar Thread para fazer backup e mostrar Jdialog com progress bar indeterminate
1 resposta
zicky23
Boa tarde, gostaria de ao tentar fechar meu sistema ele começe a fazer um backup do DB.GDB, enquanto ele copia o db eu qria que aparecesse um JDialog que crieu, depois que terminasse gostaria que fechasse o JDialog e tbm o sistema.
fiz da seguinte maneira, funcionando tá mas qro saber se ta correto.
classe carregando.java
packageGrafica;/** * * @author Wagner Vielmond */publicclasscarregandoextendsjavax.swing.JDialog{/** Creates new form carregando */publiccarregando(java.awt.Frameparent,booleanmodal){super(parent,modal);initComponents();setLocationRelativeTo(null);setResizable(false);progress.setIndeterminate(true);RunnableThreadthread=newRunnableThread();ThreadthreadR=newThread(thread);//Thread threadC = new Thread(thread);threadR.start();}
classe RunnableThread
packageGrafica;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.nio.channels.FileChannel;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.logging.Level;importjava.util.logging.Logger;classRunnableThreadimplementsRunnable{privateintcount;Stringip,data,hora;//pega a unidade instaladaFilepasta=newFile(System.getProperty("user.home"));String[]disco=pasta.toString().split(":");publicRunnableThread(){//count=1;}publicvoidrun(){try{backup_fecha();System.exit(0);// while(true){// System.out.println(count++);// Thread.sleep(1000);// }}catch(Exceptionex){Logger.getLogger(RunnableThread.class.getName()).log(Level.SEVERE,null,ex);}}publicvoidip(){// Gravando no arquivobyte[]buffer=newbyte[1000];InputStreamin;try{in=newFileInputStream(disco[0]+":\\gerenciador_extratos\\ConfigIP2.txt");in.read(buffer);Stringtemp=newString(buffer).trim().toString();ip=temp;in.close();}catch(FileNotFoundExceptione){System.out.println(e);}catch(IOExceptione){System.out.println(e);}}publicvoidbackup_fecha(){ip();SimpleDateFormatData=newSimpleDateFormat("dd-MM-yyyy");data=Data.format(newDate());SimpleDateFormatHora=newSimpleDateFormat("HH.mm");hora=Hora.format(newDate());System.out.print(data+hora);FileChanneloriChannel=null;try{oriChannel=newFileInputStream("\\"+"\\"+ip+"\\db\\DB.GDB").getChannel();}catch(FileNotFoundExceptionex){Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE,null,ex);}FileChanneldestChannel=null;try{destChannel=newFileOutputStream("\\"+"\\"+ip+"\\backup\\DB_BKP_"+data+"_as_"+hora+".GDB").getChannel();}catch(FileNotFoundExceptionex){Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE,null,ex);}try{destChannel.transferFrom(oriChannel,0,oriChannel.size());}catch(IOExceptionex){Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE,null,ex);}try{// Fecha channelsoriChannel.close();}catch(IOExceptionex){Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE,null,ex);}try{destChannel.close();}catch(IOExceptionex){Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE,null,ex);}}}
Aplicação Principal
this.addWindowListener(newWindowAdapter(){@OverridepublicvoidwindowClosing(WindowEvente){intstatus=JOptionPane.showConfirmDialog(null,"Deseja sair do programa?","Atenção",JOptionPane.YES_NO_OPTION);if(status==JOptionPane.YES_OPTION){intstatus2=JOptionPane.showConfirmDialog(null,"Deseja fazer backup?","Atenção",JOptionPane.YES_NO_OPTION);if(status2==JOptionPane.YES_OPTION){newcarregando(null,true).setVisible(true);System.exit(0);}else{System.exit(0);}}}});