Link to home
Start Free TrialLog in
Avatar of Ikelca
IkelcaFlag for Canada

asked on

need help on java GUI application

my GUI part is really simply, just a text area and one start button, the text area is to display output

when button is clicked, it will run a method which runs for about 7 mins
now the problem is that after button is clicked, entire application is frozen and waiting for that method to finish....

how could i do  to free GUI and button after button is clicked while method is running ???
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Swing is single threaded so you need to do long tasks in a background thread

SwingWorker is great for this sort of thing
i think all you have to do is after the action listener has captured the button click event, you need to invoke the other methods inside asynchronously
http://cephas.net/blog/2004/02/02/asynchronous-method-invocation-in-java/
Avatar of colr__
colr__

public void actionPerformed(ActionEvent ev){
     new Thread(){

     }
}
Something went a bit whacky there, Iw asnt finished posting!

public void actionPerformed(ActionEvent ev){
     new Thread(){
          doYourSevenMinuteTask();
     }.start();
}
Avatar of Ikelca

ASKER

hi colr

i got illegal start of type error on "this.loadFiles();" line, do u know what causes it??

public void actionPerformed(ActionEvent ev){
     new Thread(){
          this.loadFiles();
     }.start();
}
>           this.loadFiles();

try:


          loadFiles();
Sorry, just back from lunch! As objects has said, using this won't work as it would refer to the Thread object rather than the object containging the loadFiles() method.
Avatar of Ikelca

ASKER

That means I have to create a new main instance and refer loadfiles() method thru new instanc?
What about this?

public void actionPerformed(ActionEvent ev){
     new MyThread().start();
}

public class MyThread extends Thread{
    private void loadFiles(){
     .....
    }

    public void run(){
        loadFiles();
    }
}
ASKER CERTIFIED SOLUTION
Avatar of colr__
colr__

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ikelca

ASKER

I will try again and come back
Thanks
Avatar of Ikelca

ASKER

ok i tested, it worked partially, my main GUI still getting frozen a little bit