Access Swing Component infinitely from a separate thread (without "freezing" the app)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
2
  #11
Oct 16th, 2009
Change this
Originally Posted by Clawsy View Post
  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
  1. public detectUSB() {
  2. initComponents();
  3. }
and change this
  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
  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
  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
  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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: moutanna is an unknown quantity at this point 
Solved Threads: 6
moutanna moutanna is offline Offline
Light Poster
 
0
  #12
Oct 16th, 2009
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.......
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 104
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster
 
0
  #13
Oct 16th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #14
Oct 16th, 2009
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:

  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:

  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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 104
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster
 
0
  #15
Oct 16th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

Tags
component, infinite, java, loop, swing, thread

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for component, infinite, java, loop, swing, thread
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC