-1
I have a class that inherits from SwingWorker
and I’m getting information from a file .txt
and playing for the JTextArea
, and would like to go running update of JTextArea
while the loop is running, only it is not working the way I want, as it is running the entire loop and only after all the file processing .txt
that in the case has more than 100 thousand lines is updating the JTextArea
, with this the Swing ends up locked and showing nothing to the user.
I repeat, I have inherited the SwingWorker
to deal with it, but it doesn’t solve my problem.
Removing the comments from the lines of JOptionPane
in the method protected void process(List<String> pairs)
, makes the application run the way I want, IE, traverses the loop and a append()
in the JTextArea
, I did it just for demonstration.
It’s been four days that I’m researching a solution and nothing. How can I solve this problem?
I hope I was clear.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.concurrent.Worker;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author claytonpereira
*/
public class Arquivo_txt_task extends javax.swing.SwingWorker<Void,String>{
static Vector conteudo;
String data;
volatile static FileOutputStream out = null;
JFileChooser chooser = new JFileChooser();
String arquivo_csv;
String arquivo_csv_ajustado;
static JTextArea mensagem;
JFileChooser selecionaarquivo = TelaImportacaoSisobMensal.selecionaarquivo;
JTextArea converte_txt_to_csv = TelaImportacaoSisobMensal.Converte_txt_to_csv;
File arquivo = TelaImportacaoSisobMensal.arquivo;
@Override
protected Void doInBackground() throws Exception {
List<String> texto = format_txt_to_csv(arquivo);
int i = 0;
while(i<texto.size()){
publish(texto.get(i));
i++;
}
return null;
}
@Override
protected void process(List<String> pairs) {
for (String texto : pairs) {
// TelaImportacaoSisobMensal.Converte_txt_to_csv.revalidate();
TelaImportacaoSisobMensal.Converte_txt_to_csv.setVisible(true);
TelaImportacaoSisobMensal.Converte_txt_to_csv.append(texto + "\n");
//JOptionPane pane = new JOptionPane();
// pane.showMessageDialog(null, "Atualizando Registros ..........!");
System.out.print("\n" +" rodando dentro de process ........ " + texto + "==" + "\n");
}
}
public Vector format_txt_to_csv(File arquivo) throws FileNotFoundException, IOException, StringIndexOutOfBoundsException {
Vector texto = new Vector(8, 3);
try (
FileInputStream fstream = new FileInputStream(arquivo)) {
BufferedReader leitor = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = leitor.readLine()) != null) {
String livro_n = strLine.substring(0, 6);
String folha_n = strLine.substring(6, 11);
String termo_obito_n = strLine.substring(11, 21);
String data_lavr_certidao_obito = strLine.substring(21, 29);
String benef_inss_n = strLine.substring(29, 39);
String nome_falecido = strLine.substring(39, 115);
// String nome_falecido_ajustado = nome_falecido.trim();
String nome_mae_falecido = strLine.substring(115, 147);
// String nome_mae_falecido_ajustado = nome_mae_falecido.trim();
String data_Nascimento = strLine.substring(147, 155);
String data_Obito = strLine.substring(155, 163);
String cpf = strLine.substring(163, 174);
String nit = strLine.substring(174, 185);
String tipo_identifica_cartorio = strLine.substring(185, 186);
String Id_cartorio = strLine.substring(186, 200);
String line = "";
line = line + livro_n + ";" + folha_n + ";" + termo_obito_n + ";"
+ data_lavr_certidao_obito + ";" + benef_inss_n + ";"
+ nome_falecido + ";" + nome_mae_falecido + ";"
+ data_Nascimento + ";"
+ data_Obito + ";"
+ cpf + ";" + nit + ";"
+ tipo_identifica_cartorio + ";"
+ Id_cartorio + ";";
texto.add(line);
}
return texto;
}
}
The problem is that Voce is not using the class as it should be used. All incremental text programming should be done parallel to the main thread, that is, within the doinbackground, and so that you can update correctly at each iteration, pass the texts of each of the iterations to Publish and concatenate in the text field via process. As for the fast occurring, even if you do as I said, it will always occur because java runs according to the processing speed of your loop.
– Blastoise Opressor