Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on the EDT  RSS feed

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working with a rather complex dialog box that is supposed to sort data.
The dialog box opens and the data is present but the data is not sorted.
The command to sort the data is done in the last line(s) of the constructor.

Using the same code and the same location, the code was placed in a Runnable object.

Runnable sortTheData = new Runnable()
{
public void run()
{
mySorter.doTheSort();
}
};
SwingUtilities.invokeLater(sortTheData);

It works. The dialog box opens and the data is sorted.

Here are my questions.

If all Swing commands are executed via the EDT and my sort command was the last to execute why was the data NOT sorted and why did using the Runnable object work if all it is doing is running in the background?

Disclaimer: Have not worked with the Runnable object and SwingUtilities until recently.
 
Campbell Ritchie
Sheriff
Posts: 53734
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use invokeLater to start that Runnable, it will either start the EDT or plug into the EDT when it has the opportunity to. How big is the data structure you are sorting? If it is not too large, it will be sorted before the GUI appears. Remember it takes something like 20ms for a GUI to be painted.
 
Sam Gooding
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Landry wrote:Working with a rather complex dialog box that is supposed to sort data.
The dialog box opens and the data is present but the data is not sorted.
The command to sort the data is done in the last line(s) of the constructor.

Using the same code and the same location, the code was placed in a Runnable object.

Runnable sortTheData = new Runnable()
{
public void run()
{
mySorter.doTheSort();
}
};
SwingUtilities.invokeLater(sortTheData);

It works. The dialog box opens and the data is sorted.

Here are my questions.

If all Swing commands are executed via the EDT and my sort command was the last to execute why was the data NOT sorted and why did using the Runnable object work if all it is doing is running in the background?

Disclaimer: Have not worked with the Runnable object and SwingUtilities until recently.


Here's the bug in your reasoning:

all Swing commands are executed via the EDT


it's not true. The rule is all calls to methods which might effect a Swing component should be made in the EDT. That doesn't mean they ARE, even if those methods belong to Swing components. Swing methods themselves have no special status as being executed on the EDT. There are a few methods that are actually automagically dispatched in the EDT without you having to do anything, most notably repaint() and revalidate(), but they're the exceptions. It's perfectly possible to call a Swing component's method off the EDT and have the result never appear because it was never processed by the EDT.

As far as your specific example goes, the reason it didn't show up was because the dialog box with the unsorted data was "realized", which is to say it was contained by a JFrame whose paint() method had already been called. All methods that effect realized Swing components must occur on the EDT. Your sort method was effecting a realized Swing component; it therefore had to occur on the EDT.


Most components in GUIs are constructed before the JFrame is realized; We make the components, establish the container hierarchy, attach listeners then pull the trigger by adding them to the JFrame and calling pack and setVisibile on that JFrame. But this is the only reason we get away with doing all this not explicitly on the EDT . In fact, by doing this (which we all do) there are weird one-off scenarios involving worker threads which sleep which can end up causing Swing effecting methods to be executed on two different threads, the main thread and the EDT. The only take away there is that *you are not free to program anything java can do if you're writing code that effects Swing components off the EDT thread because you think it's OK because, you reason, the components involved are not realized*.

So we all do create GUIs in a theoretically dangerous way all the time. This last point is just a note, and isn't a response to your question which I answered earlier.

Takeaway- even if the components haven't been realized, to protect against weirdo scenarios and ensure that you can program anything anyway you see fit without accidentally blowing things up, make sure even Swing init code in the start of your program is run on the EDT.
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies and Sam I had to read your reply a few times but it sank it with each read.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!