| | |
Access Swing Component infinitely from a separate thread (without "freezing" the app)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
2
#11 Oct 16th, 2009
Change this
to this
and change this
to this
and change this
to this
•
•
•
•
Java Syntax (Toggle Plain Text)
usbThread thread = new usbThread(); public detectUSB() { initComponents(); thread.start(); final JLabel label = lbl_status; final String text = thread.getThreadStatus(); SwingUtilities.invokeLater( new Runnable() { public void run() { label.setText(text); label.validate(); label.repaint(); } } ); }
Java Syntax (Toggle Plain Text)
public detectUSB() { initComponents(); }
•
•
•
•
Java Syntax (Toggle Plain Text)
public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new detectUSB().setVisible(true); } }); }
Java Syntax (Toggle Plain Text)
public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new detectUSB().setVisible(true); } }); new usbThread(lbl_status).start(); }
•
•
•
•
Java Syntax (Toggle Plain Text)
public class usbThread extends Thread{ public String status="....."; usbThread() { } String getThreadStatus() { return this.status; } @Override public void run() { .... this.status="FindDrive: waiting for devices..."; .... this.status="Drive "+letters[i]+" has been plugged in"; else this.status="Drive "+letters[i]+" has been unplugged"; .... } try { Thread.sleep(100); } catch (InterruptedException e) { /* do nothing */ } } }
Java Syntax (Toggle Plain Text)
public class usbThread extends Thread{ private String status = "....."; private JLabel label; usbThread(JLabel label) { this.label = label; } private void postStatus() { SwingUtilities.invokeLater( new Runnable() { public void run() { label.setText(status); label.validate(); label.repaint(); } } ); } @Override public void run() { .... this.status="FindDrive: waiting for devices..."; postStatus(); .... this.status="Drive "+letters[i]+" has been plugged in"; else this.status="Drive "+letters[i]+" has been unplugged"; .... } postStatus(); try { Thread.sleep(100); } catch (InterruptedException e) { /* do nothing */ } } }
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
----------------------------------------------
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
•
•
Join Date: Feb 2008
Posts: 104
Reputation:
Solved Threads: 5
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!

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!
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:
And, in any case, change the usbThread constructor as follows:
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.
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)
public static void main(String args[]) { detectUSB dusb = new detectUSB(); SwingUtilities.invokeLater(new Runnable() { public void run() { dusb.initComponents().setVisible(true); } }); new usbThread(dusb.lbl_status).start(); }
And, in any case, change the usbThread constructor as follows:
Java Syntax (Toggle Plain Text)
usbThread(JLabel label) { this.label = label; setDaemon(true); }
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
----------------------------------------------
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
•
•
Join Date: Feb 2008
Posts: 104
Reputation:
Solved Threads: 5
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.
. 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. ![]() |
Similar Threads
- Load Combobox on Separate Thread (VB.NET)
- Exception in thread "main" java.lang.NoClassDefFoundError: Invaders Error (Java)
- Name the Swing Component with Date and Time (Java)
- how to relocate swing component automatically??? (Java)
- Java GUI problem - "BeanBox" swing component implementation (Java)
- Please Name this Swing Component? (Java)
- google "keyword" question (Search Engine Optimization)
- Exception in thread "QueuedExecutor" (Java)
Other Threads in the Java Forum
- Previous Thread: Packages
- Next Thread: possible loss of precision error for obtaining sum, average, etc..
| Thread Tools | Search this Thread |
Tag cloud for component, infinite, java, loop, swing, thread
.net ajax animated apple applet application apps array automation binary blackberry c++ class classes client code color component convert csv design designer detection development draw eclipse error errors event exception external file firefox foreach forms fractal game google gui gwt high hql html ibm ide image infinite input integer intellijidea8 java javafx javascript jetbrains jmf jni jsp julia key keyword linux login loop looping macosx method microsoft mysql netbeans newbie object online openjavafx panel php problem programming project projectideas python recursive security set socket software sort sql string sun swing swt test thread threads time tree ui web windows wxpython







