I have code that change the button text at run time however this does not seem to happen as required
on line 11 it should set the text on button 2 to blank
everything else work fine but it leaves the button with text on it which means that it could be clicked and try to preform the action relating to that button text, one such text could be add to database so i would end up with two entries in my mysql table which is wrong and would cause a problem later in the application
i have had a similar issue before where i wanted text "working" to be written in a text box but it did not take place until the rest of the code had run on that occasion i found a work around but this time i can not any ideas as to how to make this run in the order the appear in the code i should also say that the text on button 5 gets changed by another part of the application and thereby changing the function of button 5 which works perfectly
You are blocking the event thread. Everything non-trivial you do in the actionPerformed method should be done on a SwingWorker thread, not on the Event Thread.
For example, if you do this:
then the only thing you will see in the GUI is the final "Action Complete" text, and that will happen only after the actionPerfocmed() method finishes.
Don't use different Threads in Swing, because (like most GUI frameworks) it is not thread safe. I suggest you start with the Java™ Tutorials section about threads in Swing. That explains how SwingWorker can use other Threads.
Fred Kleinschmidt wrote:You are blocking the event thread. Everything non-trivial you do in the actionPerformed method should be done on a SwingWorker thread, not on the Event Thread.
For example, if you do this:
then the only thing you will see in the GUI is the final "Action Complete" text, and that will happen only after the actionPerfocmed() method finishes.
That's true -- but it only matters if the doSomething() method takes a significant amount of time to complete. In peter's case that method (judging from its name) updates a database, which on my computer with MySQL takes only a couple of milliseconds. You really need something which takes more than half a second or you'd hardly notice the transition to "Action Complete".
I have had a similar issue before where i wanted text "working" to be written in a text box but it did not take place until the rest of the code had run on that occasion
which suggests something taking a non-trivial amount of time.
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
a bit of art, as a gift, the permaculture playing cards