• Post Reply Bookmark Topic Watch Topic
  • New Topic

precedence  RSS feed

 
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
peter m hayward
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this seems to be an area of java i do no understand how do i go about using different threads and how will that help me
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but he did say
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!