Bom dia,
Estou tentando criar uma aplicação de importação de dados com Thread para que se possa tanto acompanhar a importação quanto poder pará-la e retomá-la.
Infelizmente mesmo usando threads a janela congela.
Já li várias soluções no fórum, mas nenhuma me permitindo o controle da thread gerada:
private void btnImportarActionPerformed(java.awt.event.ActionEvent evt) {
if( !this.txtLinha.getText().equals( "0" ) )
{
int resposta = JOptionPane.CLOSED_OPTION;
while( resposta != JOptionPane.CLOSED_OPTION )
{
resposta = JOptionPane.showConfirmDialog(this, "Deseja retomar de onde havia parado?", "Erro", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE );
if (resposta == JOptionPane.NO_OPTION)
this.txtLinha.setText("0");
}
}
if( this.txtLinha.getText().equals("0") && chkCabecalho.isSelected() )
this.txtLinha.setText("1");
if( txtArquivo.getText().length() == 0 )
JOptionPane.showMessageDialog(this, "Favor selecionar um arquivo!", "Erro", JOptionPane.WARNING_MESSAGE);
else if( txtDatabase.getText().length() == 0 )
JOptionPane.showMessageDialog(this, "Favor selecionar um banco de dados!", "Erro", JOptionPane.WARNING_MESSAGE);
else if( txtCep.getText().length() == 0 )
JOptionPane.showMessageDialog(this, "Favor selecionar um banco de dados!", "Erro", JOptionPane.WARNING_MESSAGE);
else if( this.cmbTipoBase.getSelectedIndex() == 0 )
JOptionPane.showMessageDialog(this, "Favor selecionar um tipo de banco!", "Erro", JOptionPane.WARNING_MESSAGE);
else
{
try
{
if( this.tb == null )
this.tb = ( TipoBase ) Class.forName( pckgname + '.' + this.cmbTipoBase.getSelectedItem().toString()).newInstance();
if( ( this.thread != null ) && this.thread.isAlive() )
{
int resposta = JOptionPane.CLOSED_OPTION;
while( resposta != JOptionPane.CLOSED_OPTION )
{
resposta = JOptionPane.showConfirmDialog(this, "Já existe uma importação em andamento. Parar e começar uma nova?", "Erro", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE );
if (resposta == JOptionPane.YES_OPTION)
{
this.txtLinha.setText("0");
this.txtLog.setText("");
this.thread.destroy();
}
else if (resposta == JOptionPane.NO_OPTION)
return;
}
}
else
this.txtLog.setText("");
this.tb.configurar(this.txtArquivo.getText(), this.txtDatabase.getText(), new Integer( this.txtLinha.getText() ), this.txtCep.getText() );
this.txtLog.setText( this.txtLog.getText() + "Rodando pré-importação...\n" );
if( !this.tb.preImportar() )
{
this.txtLog.setText( this.txtLog.getText() + "Erro na pré-importação!...\n" );
return;
}
if( this.chkLimpar.isSelected() )
{
this.txtLog.setText( this.txtLog.getText() + "Limpando o banco de dados...\n" );
this.tb.limpar( stm );
}
this.txtLog.setText( this.txtLog.getText() + "Rodando importação...\n" );
if( this.thread == null )
this.thread = new Thread( this.tb );
this.thread.start();
this.btnParar.setEnabled(true);
this.txtLinha.setEditable(false);
while( this.thread.isAlive() && !this.thread.isInterrupted() )
{
this.txtLinha.setText( new Integer( tb.getContador() ).toString() );
this.txtLog.setText( this.txtLog.getText() + tb.getLog() );
Thread.sleep(1000);
}
this.txtLinha.setText( new Integer( tb.getContador() ).toString() );
this.txtLog.setText( this.txtLog.getText() + tb.getLog() );
this.btnParar.setText("Parar");
this.btnParar.setEnabled(false);
this.txtLinha.setEditable(true);
if( this.tb.getStatus() )
{
this.txtLog.setText( this.txtLog.getText() + "Importação finalizada com sucesso!" );
JOptionPane.showMessageDialog(this, "Importação finalizada com sucesso!", "Aviso", JOptionPane.DEFAULT_OPTION);
}
else
{
this.txtLog.setText( this.txtLog.getText() + "Importação finalizada com problemas!" );
JOptionPane.showMessageDialog(this, "Importação finalizada com problemas!", "Erro", JOptionPane.ERROR_MESSAGE);
}
}
catch( Exception e )
{
this.txtLog.setText(this.txtLog.getText() + "\n" + e.getMessage());
e.printStackTrace();
}
}
}