• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JButton and for loop

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
Here is a strange problem....don't know how to solve this....
I am having two buttons in my JFrame....one is a "Start" Button. The other is a "Stop" Button....I have a global variable "decider" with value "1". Once if the start button is clicked, I am starting a for loop with 100 as the limit. Inside the for loop, i am undergoing some database connections and transactions....and also, i am checking, if the decider is "0", then break.....
/* ###Stopper### */
if(decider.equals("0"))
break;
/* ###Stopper### */
Once if the stop button is clicked, Iam trying to change the variable "decider" to "0". So, the for loop should get stopped abruptly....it is not happening as expected.....the for loop gets over for 100 times and then comes out....to be very clear, once if i click the "Start" button, the control goes to the Start button...(i mean a shadow)....only after the 100 times of for loop is over, the button "Stop is getting responded....
how to avoid this???
any suggestions/solutions are highly appreciated and welcome...
bye,
Sakthivel S.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is obviously a threading problem. What you should do (more or less) is:
1. click start button
2. fork threads:
2.A. AWTThread (main): e.g. disable "Start" button
2.B. create your counting thread (new, returns at once,
runs in the background)
2.C. create a thread that joins 2.B. and waits
for it to die. this thread will notify the
AWTThread (and thus causes the gui to switch
state: disabling/enabling buttons)
3. click stop button
3.A. the click happens in the AWTThread and sends
an interruption signal to 2.C which will
cause 2.B to be interrupted.
if you do any gui painting, be sure to use SwingUtilities.invokeLater or SwingUtilities.invokeAndWait unless you are 100% sure to be in the AWTThread. for heavy backend processes create another thread, so the AWTThread yon't be blocked, and freezes your UI.
For more information on interrupting threads:
http://www.javaspecialists.co.za/archive/Issue056.html
cheers
Chantal
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic