Skip to main content

GUI loop freezing GUI

2 replies [Last post]
Joined: 2008-08-23

Okay I think this is the right place to post.

Iam new to Java and am undertaking my first GUI assignment as part of a Diploma course.

I am running my program from an extended JFrame class that implements ActionListener. Everything was working fine till I created an infinate loop from within the JFrame class that is set off by pressing a button. This loop runs a simulation and should updated changes to the screen. What is happening is that the the GUI is freezing and the button to stop the simulation (which is also the button to start the simulation) freezes and nothing gets updated.

I am sure that this is a beginner level mistake.

Thanks in advance for any pointers.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-08-23

Piet I did some readying on threads and threw the thread the loop into a runnable thread. I haven't done anything about the GUI running in the EDT but it seems to be working fine now.

Any further pointers would be welcome

Joined: 2003-07-17

Yes, you are quite right: that is a beginners mistake.

You should read about swing and threading. A short resume is give in the package description of javax.swing.

To eliminate your problem quickly you should do two things:

1: All GUI activity must be controlled from the event dispatch thread (EDT). The simplest way is to invoke from your main method SwingUtilities.invokeLater(). From now on everything is running on the EDT, (unless you later decide to start some thread).

2: The time consuming task should NOT be run on the EDT (because if it would run on the EDT, the EDT can do nothing else, like responding to mouse clicks or whatever). Create a Thread with a Runnable for that purpose.

Hope this helps