• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

call expensive code inside event handler

Btn_actionPerformed(){
Btn.setEnabled(false);
txtArea.append(str1);
// some expensive code(calling someClass.someMethod()
// that someClass is already instantiated in the GUI
txtArea.append(str2);
}

when actionPerformed() is called the GUI (the JDialog class from which it`s called) is hung until the expensive code is complete and then shows the appended strings. During the execution time the textArea is not scrollable. I want the textArea to show dynamically things happening in someClass.someMethod()  if I pass a reference to it. But since it doesn`t show thefirst string str1 appended until the execution is complete, I guess I need to something else, may be run a seperate thread for someClass.someMethod()  ?

thanks,
Susanta
0
code4susant
Asked:
code4susant
  • 4
  • 2
2 Solutions
 
CEHJCommented:
Call the expensive stuff in a dedicated Thread as you way
0
 
CEHJCommented:
as you way=as you say

Use of a SwingWorker would be better still as the GUI should be updated on a different Thread (the event dispatch thread)
0
 
CEHJCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
try this:

Btn_actionPerformed(){
Btn.setEnabled(false);
txtArea.append(str1);
new Thread(new Runnable() {
public void run() {
   // some expensive code(calling someClass.someMethod()
   // that someClass is already instantiated in the GUI
   txtArea.append(str2);
}}).start();
}
0
 
code4susantAuthor Commented:
CEHJ: as always thanks for your super fast response. The hyperlink helped me understand the concept and I would use that for a detailed implementation, especially for tracing progress.

objects: thanks for your "just what i needed" reply. the example is nifty and I love it.

thanks,
Susanta
0
 
CEHJCommented:
:-)

The second code shouldn't be used for the reason i gave earlier: the gui should be updated on the EDT
0
 
objectsCommented:
> The second code shouldn't be used for the reason i gave earlier: the gui should be updated on the EDT

thats completely incorrect, feel free to test the code and you'll find it works just fine.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now