Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Button enabled property not refreshing on the panel

 
Nischal Topno
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try calling validate() on the frame.
 
Nischal Topno
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nischal Topno
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic