• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Looking For A Rule of Thumb

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

When writing my GUI code on various projects, I sometimes run into problems updating the state of components. For instance, in one project, I may have no problem using .setEnabled(false) and the GUI updates that change. Using what I think is the same process on the next project, I use that .setEnabled(false) and the GUI is never updated.

I run the GUI using EventQueue.invokeLater(new Runnable()......... and run the time consuming work of my code in a separate thread I create just for that work.

So my question is this: is there a rule of thumb to follow in planning how the GUI gets updated, or can I just call .setEnabled(false) in a willy-nilly fashion? I wonder if I have just been lucky to have this work at all in the past.

As a side question, how prevalent is the use of Thread.sleep to give other things a chance to run? Is this equivalent to a hack to get code to run, or is it accepted practice?

Thanks for the advice and help

BD
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show an example of when setEnabled( false ) or ( true ) does not affect the specified component?
 
Bd Howard
Greenhorn
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know if Eclipse Indigo (Mac) is buggy dealing with Swing stuff? The reason I ask is that when I changed my helper method (it just sets all of the components I want disabled to .setEnabled(false) ) to static, the GUI updated as it should. I then removed the static modifier to see if it would fail again. It did not fail, and worked as it should.

Maybe I wasn't holding my mouth right when I typed in the code. :-)

Any opinions on my side question I asked above? The reason I ask is that I wondered if I needed to sleep the thread for a moment to kickstart something else so my GUI would update.

Greg, I guess posting code is moot now. I wondered how to post code when I started this thread in that should I post code that works for me, and then post the code that doesn't? I just decided to try to ask the question in a way that would elicit a "general" answer. Thanks for the response though. :-)

BD
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frankly, it's sometimes hard to answer general questions, because there are frequently exceptions. For instance, revealing that you're working on a Mac completely changes the solution set or how I'd approach solving your problems or answering your questions. The latest version of Java 7 is the first where Oracle has been allowed to release a specific Mac version. I would run, not walk, to download and install that as quickly as possible while keeping the old Mac version an option to discover what problems are solved or created.

In general, Swing is not thread safe. Sleeping the event-dispatching thread (EDT) is not a good idea in a Swing application. Will it cause the world to end or the application to crash? Probably not, but it's not recommended. Here's an article on the topic you may find useful.
 
Bd Howard
Greenhorn
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that link Greg. I've read a bunch on Threads, but it has not sunk in completely yet. You could say I know just enough about threads to burn the house down. :-)
I have not read the page you linked as of yet, but after I finish this post, I'll do just that.

I do have 1.7 on my system, and I used it once in order to try out the new FileWalker. I have been sticking to 1.6 though. Is there so much difference (at least on the Mac) between 1.6 and 1.7 that I should make the switch to 1.7 permanent? In my classes at school, 1.6 has been the rule.

Thanks again for taking the time to give me some answers to my questions.

BD
 
Bd Howard
Greenhorn
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That link gave me some good info that I will definitely use. Thanks. :-)
reply
    Bookmark Topic Watch Topic
  • New Topic