• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame freezing when a Start button is added

 
John Danek
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Oh, thanks for the response. I think I came across the same cause as you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!