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.

Como faço isso, é com thread?

valeu

1 Resposta

zicky23

fiz da seguinte maneira, funcionando tá mas qro saber se ta correto.

classe carregando.java

package Grafica;

/**
 *
 * @author Wagner Vielmond
 */
public class carregando extends javax.swing.JDialog {

    /** Creates new form carregando */
    public carregando(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        setResizable(false);
        progress.setIndeterminate(true);
        RunnableThread thread = new RunnableThread();
        Thread threadR = new Thread(thread);
        //Thread threadC = new Thread(thread);

        threadR.start();
    }

classe RunnableThread

package Grafica;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

class RunnableThread implements Runnable {
    private int count;
    String ip,data,hora;
    //pega a unidade instalada
    File pasta = new File(System.getProperty("user.home"));
    String[] disco = pasta.toString().split(":");


    public RunnableThread(){
        //count=1;
    }

    public void run(){
        try {
            backup_fecha();
            System.exit(0);
//            while(true){
//                System.out.println(count++);
//                Thread.sleep(1000);
//            }

        } catch (Exception ex) {
            Logger.getLogger(RunnableThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void ip(){
        // Gravando no arquivo
        byte[] buffer = new byte[1000];
        InputStream in;
        try {
         in = new FileInputStream(disco[0]+":\\gerenciador_extratos\\ConfigIP2.txt");
         in.read(buffer);
         String temp = new String(buffer).trim().toString();
         ip = temp;
         in.close();
        } catch (FileNotFoundException e) {
         System.out.println(e);
        }catch (IOException e) {
         System.out.println(e);
        }
    }

    public void backup_fecha(){
        ip();
        SimpleDateFormat Data = new SimpleDateFormat("dd-MM-yyyy");
        data = Data.format(new Date());
        SimpleDateFormat Hora = new SimpleDateFormat("HH.mm");
        hora = Hora.format(new Date());
        System.out.print(data+hora);

        FileChannel oriChannel = null;
        try {
            oriChannel = new FileInputStream("\\"+"\\"+ip+"\\db\\DB.GDB").getChannel();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
        FileChannel destChannel = null;
        try {
            destChannel = new FileOutputStream("\\"+"\\"+ip+"\\backup\\DB_BKP_"+data+"_as_"+hora+".GDB").getChannel();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            destChannel.transferFrom(oriChannel, 0, oriChannel.size());
        } catch (IOException ex) {
            Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            // Fecha channels
            oriChannel.close();
        } catch (IOException ex) {
            Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            destChannel.close();
        } catch (IOException ex) {
            Logger.getLogger(AplicacaoPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Aplicação Principal

this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                int status = JOptionPane.showConfirmDialog(null,"Deseja sair do programa?","Atenção",JOptionPane.YES_NO_OPTION);
                if (status == JOptionPane.YES_OPTION)
                {
                    int status2 = JOptionPane.showConfirmDialog(null,"Deseja fazer backup?","Atenção",JOptionPane.YES_NO_OPTION);
                    if (status2 == JOptionPane.YES_OPTION)
                    {
                        new carregando(null, true).setVisible(true);
                                    System.exit(0);
                    }else{
                        System.exit(0);
                    }
                } 
            }
        });
Criado 20 de janeiro de 2012
Ultima resposta 22 de jan. de 2012
Respostas 1
Participantes 1