Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange behaviour with a JButton  RSS feed

 
Roberto De Giuli
Greenhorn
Posts: 17
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a problem with a simple application I'm developing. There's a JButton with a related ActionPerformed event. When I click the JButton an InputDialog ask the user to enter a URL (of a TXT file on a website), then the application tries to read the file content and (at the moment) tries to print it.

The ActionPerformed code is the following:



If the user enters a URL (and doesn't click on Cancel) the text of the JButton should change to "...WAIT...". If something goes wrong or when the operation is completed, the "finally" clause should restore the original text for the JButton.

Here's the problem: the first line of code in the try section should change the text value of the JButton but actually it doesn't happen! The JButton text only changes a moment before the connection times out (it's normal, I'm behind a firewall and I have to get the connection timeout.

I can't figure out why, any suggestion is more than appreciated. Thank you.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The text change as well as the attempt to read the file is happening on the same thread. Solution: Spawn a new thread and move your file read logic onto it.
Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/
 
Roberto De Giuli
Greenhorn
Posts: 17
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:The text change as well as the attempt to read the file is happening on the same thread. Solution: Spawn a new thread and move your file read logic onto it.
Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/


Thank you very much! It's more tricky than I supposed but it's an interesting thing to learn.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto De Giuli wrote: It's more tricky than I supposed but it's an interesting thing to learn.

Not really once you figure out what happens under the hood
Check out this faq entry which provides a solution to a problem similar to yours https://www.coderanch.com/how-to/java/JProgressBarDoesntUpdate That should help you fix yours
 
Roberto De Giuli
Greenhorn
Posts: 17
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Roberto De Giuli wrote: It's more tricky than I supposed but it's an interesting thing to learn.

Not really once you figure out what happens under the hood
Check out this faq entry which provides a solution to a problem similar to yours https://www.coderanch.com/how-to/java/JProgressBarDoesntUpdate That should help you fix yours


This link is very interesting, thank you very much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!