Reputation: 111
I have a problem in moving from a java console to a GUI program.
My console program loads all the words from a dictionary. It then uses each word in turn to decrypt a cipher, displaying the deciphered text on the screen for the first word, then for the second word and so on.
When I write the GUI program, I have the command
jTextArea.append(decipherment);
but nothing is displayed until the program has deciphered with every word and then all the decipherments are displayed together, rather than one by one as I want.
The structure of my GUI program includes a button with the code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
decrypt();
}
and then there is a section:
public void decrypt(){
...
}
that contains the code to load the dictionary words, do the deciphering with each word
and display each decipherment with the method call: jTextArea.append(decipherment);
But, as mentioned, the individual decipherments are not displayed. Rather the program runs to the end and then displays all the decipherments together.
After reading other threads I have the feeling that I am not writing the GUI program correctly but I haven’t found what my mistake is. Help would be appreciated.
Upvotes: 1
Views: 133
Reputation: 324118
Read the section from the Swing tutorial on Concurrency to understand why the GUI is being blocked.
You can use a SwingWorker
for your background Thread and then publish
the results as you go so the GUI can be updated.
Upvotes: 4
Reputation: 12332
Your decrypting is most likely running in the same thread as your GUI and is locking it up. Try spawning a new thread to run your decryption, then update your GUI in the Swing thread.
Try something like this:
Thread workThread = new Thread(new Runnable() { // run process in new thread
public void run() {
decrypt();
}
});
workThread.start();
How to update your Swing thread:
EventQueue.invokeLater(new Runnable() { // update Swing thread here
public void run() {
jTextArea.append(decipherment);
}
});
Upvotes: 4