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

Discussion in 'Java' started by C-man, Aug 2, 2004.

  1. C-man

    C-man Guest

    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
     
    C-man, Aug 2, 2004
    #1
    1. Advertising

  2. C-man

    David Hilsee Guest

    "C-man" <> wrote in message
    news:I4zPc.16428$yT2.14347@clgrps13...
    > 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.

    --
    David Hilsee
     
    David Hilsee, Aug 2, 2004
    #2
    1. Advertising

  3. C-man

    C-man Guest

    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


    "David Hilsee" <> wrote in message
    news:...
    > "C-man" <> wrote in message
    > news:I4zPc.16428$yT2.14347@clgrps13...
    > > 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.
    >
    > --
    > David Hilsee
    >
    >
     
    C-man, Aug 3, 2004
    #3
  4. C-man

    David Hilsee Guest

    "C-man" <> wrote in message
    news:ZmCPc.17745$yT2.15887@clgrps13...
    > 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.

    --
    David Hilsee
     
    David Hilsee, Aug 3, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip Townsend

    button focus and pressing enter

    Philip Townsend, Aug 14, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    8,791
  2. Philip Townsend

    button focus and pressing enter

    Philip Townsend, Aug 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,269
    Philip Townsend
    Aug 25, 2003
  3. Dan Brussee

    Default button - pressing Enter

    Dan Brussee, Feb 19, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    479
  4. Robocop
    Replies:
    2
    Views:
    456
    Andrew Hobbs
    Feb 22, 2004
  5. albert kao
    Replies:
    1
    Views:
    2,070
    marisa lee
    Apr 10, 2010
Loading...

Share This Page