wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Looking For A Rule of Thumb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Looking For A Rule of Thumb" Watch "Looking For A Rule of Thumb" New topic
Author

Looking For A Rule of Thumb

Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
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


I've got just enough Java knowledge to royally screw everything up. :-)
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Can you show an example of when setEnabled( false ) or ( true ) does not affect the specified component?


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Bd Howard
Greenhorn
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
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

Joined: Oct 24, 2010
Posts: 557
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
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
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
Ranch Hand

Joined: Mar 30, 2012
Posts: 80
That link gave me some good info that I will definitely use. Thanks. :-)
 
Don't get me started about those stupid light bulbs.
 
subject: Looking For A Rule of Thumb
 
Similar Threads
create a new autoincremented value from the database to display in textbox
Worker Threads
Rule of thumb with Struts (for Neal Ford)
How to notify a MenuItem I have change its properties?
Buttons...