Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2004

Tip: Looking for answers? Try searching our database.

How do i start a thread when button is pressed?

Thread view: 
Tom Andr? Cook - 20 Jul 2004 02:12 GMT
Hi, I wonder if someone could give me an example on how to write the code
when i want to start a thread when a button is pressed? The button invokes a
method with some quit heavy computations, so I want a new thread to do the
job, so that the main thread can do some repainting for me... like a
progressbar for example.

Tom Cook
Shane Mingins - 20 Jul 2004 02:49 GMT
> Hi, I wonder if someone could give me an example on how to write the code
> when i want to start a thread when a button is pressed? The button invokes a
> method with some quit heavy computations, so I want a new thread to do the
> job, so that the main thread can do some repainting for me... like a
> progressbar for example.

Did you look for an answer on the Sun Site to begin with?

Like this
http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html

Shane

Signature

"If you really want something in this life, you have to work for it - Now
quiet, they're about to announce the lottery numbers!"

Rogan Dawes - 20 Jul 2004 08:10 GMT
>>Hi, I wonder if someone could give me an example on how to write the code
>>when i want to start a thread when a button is pressed? The button invokes
[quoted text clipped - 11 lines]
>
> Shane

Or look at SwingWorker, which I think is really what the original poster
was looking for.

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

Rogan
Signature

Rogan Dawes

*ALL* messages to discard@dawes.za.net will be dropped, and added
to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"

Shane Mingins - 20 Jul 2004 21:31 GMT
> Or look at SwingWorker, which I think is really what the original poster
> was looking for.

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

Which is demonstrated with the progress bar and also referenced from the
Progress Bar Demo page.

Is it too much to ask that someone do a little reading and work?

Shane

Signature

"" -- Marcel Marceau

Tom Andr? Cook - 23 Jul 2004 09:47 GMT
Thankyou for the tip, but I guess I should not have said progressbar,
because I want my own animation while some other computing is happening, and
I tried to start a thread inside the actionPerformed method, but it did not
work, and I thought maybe I mis-spelled some of the code, I have not
experience with threads because I am more used to code in C++. I am studying
Java at the moment, and I have not yet grasped the way java are coded, so
program examples, syntax examples are most welcome. I have fixed the problem
now thanks to the example that was given me in one of the replies.

Tom Andr? Cook

> > Or look at SwingWorker, which I think is really what the original poster
> > was looking for.

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

> Which is demonstrated with the progress bar and also referenced from the
> Progress Bar Demo page.
[quoted text clipped - 5 lines]
> --
> "" -- Marcel Marceau
Morten Alver - 20 Jul 2004 12:55 GMT
> Hi, I wonder if someone could give me an example on how to write the code
> when i want to start a thread when a button is pressed? The button invokes a
> method with some quit heavy computations, so I want a new thread to do the
> job, so that the main thread can do some repainting for me... like a
> progressbar for example.

The replies above should answer your question.

If you just want a quick (and dirty?) solution, you can just do like
this in your actionPerformed method:

(new Thread() {
    public void run() {
        [put the time-consuming code here]
    }
}).start();

--
Morten
Tom Andr? Cook - 20 Jul 2004 12:14 GMT
ThankX :-) the thread works very well now! Since I am new to java syntax, I
messed up my code why it did not work before. I have noted you saying quick
and (dirty) the way it is implemented now, so I will dig my nose into
exception handling to make the whole thing better(I hope!?) I would like to
know if you agree that the way to go now is exception handling to make a
more thorough code?

Tom Andr? Cook

> > Hi, I wonder if someone could give me an example on how to write the code
> > when i want to start a thread when a button is pressed? The button invokes a
[quoted text clipped - 15 lines]
> --
> Morten
Morten Alver - 20 Jul 2004 15:48 GMT
> ThankX :-) the thread works very well now! Since I am new to java syntax, I
> messed up my code why it did not work before. I have noted you saying quick
> and (dirty) the way it is implemented now, so I will dig my nose into
> exception handling to make the whole thing better(I hope!?) I would like to
> know if you agree that the way to go now is exception handling to make a
> more thorough code?

I was not thinking about anything specific when saying quick and dirty.
But in a large application you may want to systematically use threading
to offload the event handling thread (to keep the interface responsive)
- and in this case a class like SwingWorker would help you do this
consistently, and probably gives you some additional advantages (I
haven't used SwingWorker myself, so I don't know specifics).

In any case, exception handling is definitely something you have to learn.

--
Morten


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.