Petr Dvorak wrote:
> Gregg Wonderly wrote:
>> Petr Dvorak wrote:
>>> Do you mean something like this (the simplest/likely-dumbest
>>> implementation possible)? You might play with the appearance, output
>>> format, etc... The key portion of code:
>>>
>>> public NewJFrame() {
>>> initComponents();
>>> jLabel1.setText(Calendar.getInstance().getTime().toString());
>>>
>>> _iTimer = new Timer(true);
>>> _iTimer.scheduleAtFixedRate(new TimerTask() {
>>>
>>> @Override
>>> public void run() {
>>> jLabel1.setText(Calendar.getInstance().getTime().toString());
>>> }
>>> }, 1000, 1000);
>>> }
>>
>> You must code this with some variant of the following,
>>
>> public void run() {
>> SwingUtilities.invokeAndLater( new Runnable() { public void
>> run() {
>> jLabel1.setText(Calendar.getInstance().getTime().toString());
>> } } );
>> }
>>
>> to make sure that you don't futz with the AWT/swing environment? A
>> non event thread should never be calling into any GUI component
>> methods! You could use invokeAndWait() as well, but that requires
>> handling interrupted exceptions.
>>
>> Gregg Wonderly
>
> Thanks for the good remark (suggestion to use of
> SwingUtilities.invokeLater) - I said it is simplest and likely-dumbest
> implementation possible. I still think that use of my simple code is
> good enough for this demo app (the application should do nothing more
> that the clock), but generally you are right...
What happens Petr, is that examples turn into "demos" which turn into "small
examples" which get to have a life of their own as "the way" to do things.
The Swing/AWT platform programming model (or lack thereof) causes this small
detail of "context management" to become a real headache for those that don't
know that it is a requirement for GUI applications to manage this issue.
I think its best to deal with this issue up front to make sure people go down
the right paths of abstracting their interactions with the real world vs
updating components so that they have this one knocked out from the start.
Maybe someday we'll get a GUI frame work design that actually hides this so that
methods like this and classes like SwingWorker are a distant memory...
Gregg Wonderly