Reputation: 1807
I have a strange behaviour, when I click the start button, I want a JTextField called status to change its text to show the user that the file is processing. But instead the app just start processing the .txt files and the status is never updated during the file processing period.
Code:
private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
statusTextField.setText("Processing files.");
int startLine = Integer.parseInt(startLineTextField.getText());
try {
if (selectedFile != null) {
if (selectedFile.getName().contains(".txt")) {
mainController.start(new FileInputStream(selectedFile), startLine);
statusTextField.setText("Finished !");
mainController.displayFoundAccounts();
} else if (selectedFile.getName().contains(".zip")) {
ZipFile zipFile = new ZipFile(selectedFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
InputStream inputStream = zipFile.getInputStream(entry);
mainController.start(inputStream, startLine);
}
statusTextField.setText("Finished !");
mainController.displayFoundAccounts();
}
} else {
System.out.println("No file selected.");
}
} catch (Exception ex) {
System.out.println("Error: " + ex);
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
statusTextField.setText("Stopped.");
}
}
I'm not sure where I am wrong since statusTextField.setText("Processing files.");
is called on the first line.
Upvotes: 1
Views: 39
Reputation: 9100
You should process the files on a different thread, not on EDT. Probably this is the reason why the update is not visible. For example:
private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
statusTextField.setText("Processing files.");
int startLine = Integer.parseInt(startLineTextField.getText());
new Thread() {
public void run() {
try {
if (selectedFile != null) {
...
SwingUtilities.invokeLater(new Runnable() {
public void run() {
statusTextField.setText("Finished !");
}
});
} catch (Exception ex) {
...
}
}.start();
}
Upvotes: 2