How do I start a thread by pressing a button? Simple hey?

C

C-man

Basically I have this program that will traverse a list of files in a
directory and will rename each file say to all upper or all lower case or
whatever. I want to be able to select a few options from say a radio button
options panel, then hit start to traverse and rename the files. I was told
that it is a bad idea to put much code in an action listener cuz it is
suppose to slow the system down greatly. So how do I handle the event in a
way that I can invoke a thread class from my main class not from within the
action listener class? Or if I say call a thread class from within the
action listener will that be sufficient?

Thanks
Cleave
 
D

David Hilsee

C-man said:
Basically I have this program that will traverse a list of files in a
directory and will rename each file say to all upper or all lower case or
whatever. I want to be able to select a few options from say a radio button
options panel, then hit start to traverse and rename the files. I was told
that it is a bad idea to put much code in an action listener cuz it is
suppose to slow the system down greatly. So how do I handle the event in a
way that I can invoke a thread class from my main class not from within the
action listener class? Or if I say call a thread class from within the
action listener will that be sufficient?

It can be a bad idea to execute a long-running task without a background
thread because the GUI will stop responding while your long-running task
executes. If you want information on using threads in your GUI-based
application, read this:

http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html

Pay close attention to the SwingWorker class and the link to general
information on threads.
 
C

C-man

That is exactly the thing I am looking for. So now say you have another
button to stop the current time consuming task. how do you do that with the
swingWorker. Thanks
 
D

David Hilsee

C-man said:
That is exactly the thing I am looking for. So now say you have another
button to stop the current time consuming task. how do you do that with the
swingWorker. Thanks

You can do a search for example code, but usually the approach taken
involves a boolean whose access is synchronized.

class MySwingWorker, etc {
private static class SynchedFlag {
private boolean flag = false;
public synchronized void set(){ flag = true; }
public synchronized boolean get() { return flag; }
}
private SynchedFlag stopFlag = new SynchedFlag();
public void requestStop() {
stopFlag.set();
}

}

When the button is pressed, set the flag (in the above code, that's the
requestStop() method). Then, have the background thread keep checking the
flag as it is doing its work (e.g. after every file it processes). When
stopFlag.get() returns true, give up the processing and exit the run()
method. There are other ways to do the same thing, but this is the way I
like, mostly because it looks the most readable to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top