It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Button enabled property not refreshing on the panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Button enabled property not refreshing on the panel" Watch "Button enabled property not refreshing on the panel" New topic
Author

Button enabled property not refreshing on the panel

Nischal Topno
Ranch Hand

Joined: Nov 24, 2001
Posts: 45
Hi,
I have a frame. On the frame is a panel with three buttons. In one of the button's actionperformed method, I am setting this button's enabled property to false and the other two button's enabled property to true (which are set to false initially).
Then the main thread is made to sleep for 10000 milli secs.
Even though I have put a repaint() after the three setEnabled() property, there is no change in the buttons enabled state during the execution of the program, after the first button is clicked. The pressed button remains pressed and the enabled property of the other two buttons remain the same.
How can I over come this problem.
All the above components are swing.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Try calling validate() on the frame.
Nischal Topno
Ranch Hand

Joined: Nov 24, 2001
Posts: 45
Originally posted by Paul Stevens:
Try calling validate() on the frame.

Tried the above but it doesn't help
i am attaching the code snipped
void jBtnProcess_actionPerformed(ActionEvent e) {
StatementProcessing sp = new StatementProcessing();
String sMessageBuffer;

jBtnSave.setEnabled(true);
jBtnProcess.setEnabled(false);
//This is what i added on your advice
this.validate();
try {
Thread.sleep(10000);
}catch(InterruptedException IntExc) {
}
System.out.println("Hello");
}
Only after i see "Hello", the state of the button changes, but till then the process has finished.
The class on which this is written extends the JFrame and is the frame that getvisible.
I am using a JBuilder4 and used the action performed event of the jButton in the design window.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Your Thread.sleep() is interrupting the repaint of the frame... since it doesn't seem to serve any purpose in the code ( ) you can take it out and it should work fine...

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Nischal Topno
Ranch Hand

Joined: Nov 24, 2001
Posts: 45
I have put the sleep in place of the original processing to test my problem. The orignal code is something like objectX.startProcessing();
in place of Thread.sleep();
What i want is, once the user clicks the button, this button should get disabled (so that the user cannot click it again) and make the other button enabled. Then i am calling the objectX.startProcessing(). What i found that, till the objectX.startProcessing() returns control to this class, the buttons (or for that matter frame) remains in the same state, and the pressed button remains pressed.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Is objectX using a seperate thread? If not, then the problem is that objectX is hogging the current thread. Make objectX or your program multi-threaded to get around that....

Here's an example showing how to make your code multi-threaded...


Hope this helps...

-Nate
dan moore
Ranch Hand

Joined: Nov 04, 2001
Posts: 68
hi Nischal,
Originally posted by Nischal Topno:
Hi,
...
Then the main thread is made to sleep for 10000 milli secs.
...

for a little further explanation if Nate don't mind, the "main thread" that does both the call to actionPerformed() (or any other event handlers) and the repainting of the dialog is the AWT event thread. sleeping, blocking or doing lengthy processing on this thread is discouraged as Swing relies on this thread for its processing - hogging it effectively hangs the UI.
just beware that if you start calling into Swing from Nate's new thread, just be aware that Swing is not thread-safe and it can have unpredictable effects.
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
cheers, dan.


dan moore, infomatiq ltd.<br />email dan@infomatiq.co.uk<br /><a href="http://www.infomatiq.co.uk" target="_blank" rel="nofollow">http://www.infomatiq.co.uk</a>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Button enabled property not refreshing on the panel