• Post Reply Bookmark Topic Watch Topic
  • New Topic

unexpected behavior of JButton  RSS feed

 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The attached code demonstrates a problem I am having with my Swing GUI:
I expect that when I click on the showDialog button the dialog will appear with "initial text" in the textField. My intention was to click on the changeText button in the dialog, the textField will contain "new text" and the showDialog button will disappear.
Instead, when I click on the showDialog button, the dialog appears with "new text" in the textField and the showDialog button disappears. That is, it acts as if I had already clicked on the changeText button. Can anyone explain to me what is going on?
Regards,
Joe
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at lines 30 and 44 in the code you posted. I don't think you really meant to do that.
 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that certainly was embarrassing! Thanks for looking at the code. I fixed the oversight you pointed out and that sscce executed as expected. However, my more extensive application still does not. So I examined further to see where it was failing and I find that the issue seems to involve threads started in the action performed by the button. I have added a simple example to my code which illustrates this: (lines 48 through 52)

again, I expect that when one clicks on the changeText button the textField text will change instantly and the showDialog button will disappear instantly but instead neither of those things happen until the 10 seconds delay have elapsed.???
Regards,
Joe

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any threads being started, but at any rate your code blocks for 10 seconds inside that actionPerformed method. You should never block the Swing thread like that.

You didn't say why you needed to block for 10 seconds, but you would probably benefit by reading the Swing concurrency tutorial. It should explain why Swing's display procedures are more complicated than what you thought and why you should be careful about running other threads in your Swing application, and also how you can run those other threads without making a mess of things.
 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the reply. Again, I have posted very poor example code to illustrate my program. My actual application does indeed start another thread in the action performed by clicking on a JButton. The reason I inserted the blocking code in my sscce is because it exhibits behavior similar to my application: namely, method calls that seemingly should execute BEFORE the blocking code are apparently executing AFTER the blocking code. (In my actual application, method calls that seemingly should execute before starting a thread appear to be executing after the thread finishes).
I've begun to look at the swing concurrency tutorial as you suggest. I will try using the methodology presented there, in particular the SwingWorker class. I will post back and report what I find.
I appreciate your help.
Joe
 
Les Morgan
Rancher
Posts: 752
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,

You are assuming that the only action that the ActionListener will see is the action of the button being clicked, but in reality there are several ActionEvent generating actions that go on during creation of a GUI, several of which you may be picking up with your very open ActionListener.
Narrow that scope of that ActionListener a bit by seeing who the parent of the action is, and if it is not the button, then you should ignore that action.

Les
 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Les. I'll give it a try.
Joe
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!