944,085 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3323
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 16th, 2009
2
Re: Access Swing Component infinitely from a separate thread (without "freezing" the app)
Change this
Click to Expand / Collapse  Quote originally posted by Clawsy ...
Java Syntax (Toggle Plain Text)
  1. usbThread thread = new usbThread();
  2.  
  3. public detectUSB() {
  4. initComponents();
  5.  
  6. thread.start();
  7.  
  8.  
  9. final JLabel label = lbl_status;
  10. final String text = thread.getThreadStatus();
  11.  
  12. SwingUtilities.invokeLater(
  13. new Runnable() {
  14. public void run() {
  15. label.setText(text);
  16. label.validate();
  17. label.repaint();
  18. }
  19. }
  20. );
  21. }
to this
Java Syntax (Toggle Plain Text)
  1. public detectUSB() {
  2. initComponents();
  3. }
and change this
Quote ...
Java Syntax (Toggle Plain Text)
  1. public static void main(String args[]) {
  2. java.awt.EventQueue.invokeLater(new Runnable() {
  3. public void run() {
  4. new detectUSB().setVisible(true);
  5. }
  6. });
  7. }
to this
Java Syntax (Toggle Plain Text)
  1. public static void main(String args[]) {
  2. java.awt.EventQueue.invokeLater(new Runnable() {
  3. public void run() {
  4. new detectUSB().setVisible(true);
  5. }
  6. });
  7. new usbThread(lbl_status).start();
  8. }
and change this
Quote ...
Java Syntax (Toggle Plain Text)
  1. public class usbThread extends Thread{
  2. public String status=".....";
  3. usbThread()
  4. {
  5.  
  6. }
  7.  
  8. String getThreadStatus()
  9. {
  10. return this.status;
  11. }
  12.  
  13. @Override
  14. public void run() {
  15. ....
  16. this.status="FindDrive: waiting for devices...";
  17. ....
  18. this.status="Drive "+letters[i]+" has been plugged in";
  19. else
  20. this.status="Drive "+letters[i]+" has been unplugged";
  21. ....
  22. }
  23. try { Thread.sleep(100); }
  24. catch (InterruptedException e) { /* do nothing */ }
  25. }
  26. }
to this
Java Syntax (Toggle Plain Text)
  1. public class usbThread extends Thread{
  2. private String status = ".....";
  3. private JLabel label;
  4. usbThread(JLabel label) {
  5. this.label = label;
  6. }
  7.  
  8. private void postStatus() {
  9. SwingUtilities.invokeLater(
  10. new Runnable() {
  11. public void run() {
  12. label.setText(status);
  13. label.validate();
  14. label.repaint();
  15. }
  16. }
  17. );
  18. }
  19.  
  20. @Override
  21. public void run() {
  22. ....
  23. this.status="FindDrive: waiting for devices...";
  24. postStatus();
  25. ....
  26. this.status="Drive "+letters[i]+" has been plugged in";
  27. else
  28. this.status="Drive "+letters[i]+" has been unplugged";
  29. ....
  30. }
  31. postStatus();
  32. try { Thread.sleep(100); }
  33. catch (InterruptedException e) { /* do nothing */ }
  34. }
  35. }
Last edited by masijade; Oct 16th, 2009 at 7:13 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 16th, 2009
0
Re: Access Swing Component infinitely from a separate thread (without "freezing" the app)
Thefact is the plaggedIn variable and isDrivev[i] are always equal so you the code inside the for bloc is never executed:
if you try this:
if(drives[i].canRead()) {
this.status.............
}else{
this.status.......
}
Reputation Points: 16
Solved Threads: 57
Posting Whiz
moutanna is offline Offline
385 posts
since Oct 2009
Oct 16th, 2009
0
Re: Access Swing Component infinitely from a separate thread (without "freezing" the app)
THANK YOU SO MUCH FOR SOLVING MY PROBLEM. I guess not only me was searching for this.
One note for the master : when said to add "new usbThread(lbl_status).start();" to the main function. You cannot cause main is static (you get build error). So I just started it after " initComponents();".

THANK YOU SO MUCH!
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Oct 16th, 2009
0
Re: Access Swing Component infinitely from a separate thread (without "freezing" the app)
Sorry, brainfart.

Starting after initComponents (if done within the SwingUtilities) is bad as you are kicking it off from the Swing Event Thread, and you should, really, avoid that, if possible. Try it this way. Remove the "detectUSB" constructor and then change the main as follows:

Java Syntax (Toggle Plain Text)
  1. public static void main(String args[]) {
  2. detectUSB dusb = new detectUSB();
  3. SwingUtilities.invokeLater(new Runnable() {
  4. public void run() {
  5. dusb.initComponents().setVisible(true);
  6. }
  7. });
  8. new usbThread(dusb.lbl_status).start();
  9. }

And, in any case, change the usbThread constructor as follows:

Java Syntax (Toggle Plain Text)
  1. usbThread(JLabel label) {
  2. this.label = label;
  3. setDaemon(true);
  4. }

To ensure that the thread dies properly.

Edit: And, not intending to nitpick, it is standard Java programming convention to capitalise Classes so those class names should be DetectUSB and USBThread.
Last edited by masijade; Oct 16th, 2009 at 9:55 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 16th, 2009
0
Re: Access Swing Component infinitely from a separate thread (without "freezing" the app)
Yes. Thank you . These are really good hints and valuable advices. (but I put " new usbThread(lbl_status).start();" in formWindowOpened event cause... again: Builder said 'non-static variable lbl_status cannot be referenced from a static context'... sorry ). Thanks. Now I even optimized my application.
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Packages
Next Thread in Java Forum Timeline: possible loss of precision error for obtaining sum, average, etc..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC