Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JFrame freezing when a Start button is added

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

My program has a SimController class which holds most of the logic for executing a simulation run. Before, it was in the method startSimulation(), which the constructor ran. The first thing this code does is fire a JOptionPane message. Upon clicking OK, the code executes. We now want to control when the startSimulation() method runs by adding a Start Simulation JButton to the main frame.

So, I added a Start button to the frame, registered it with the "ButtonListener" in the JFrame class, and declared that when the Start button is pressed, it should fire the simController object's startSimulation() method. This appears to work up to a point.

The JOptionPane still comes up, but once you click OK, it hangs up until the simulation run is complete. The real time updating of the various components (mostly text fields) doesn't work, and the program basically freezes until the run is over. If we revert to the old way (having the constructor automatically execute startSimulation(), it works fine.

Does anyone know what the problem might be? Thanks for any help you can give!!

P.S. The program runs in a single thread. Might this be the problem? I would prefer to keep it a single thread if possible.
 
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The program runs in a single thread. Might this be the problem?



Yes.

I would prefer to keep it a single thread if possible.



A single Thread is fine as long as that thread is NOT the EDT (Event Dispatch Thread) because that will prevent the GUI from painting and responding to events.

Read the section from the Swing tutorial on Concurrency
 
John Danek
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I found the problem, and it is described at: http://java.sun.com/docs/books/tutorial/uiswing/events/generalrules.html

Because all drawing and event-listening methods are executed in the same thread, a slow event-listener method can make the program seem unresponsive and slow to repaint itself. If you need to perform some lengthy operation as the result of an event, do it by starting up another thread



Lame. Ah well.
 
John Danek
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, thanks for the response. I think I came across the same cause as you.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic