Threads

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

Join Date: May 2009
Posts: 25
Reputation: evant8950 is an unknown quantity at this point 
Solved Threads: 0
evant8950 evant8950 is offline Offline
Light Poster

Threads

 
0
  #1
May 7th, 2009
I'm working on a program to upload some files to an ftp. I created a thread the starts the upload when the upload button is pushed. When the upload button is pushed the GUI freezes because it is uploading. What change would I have to make to get the GUI responsive so I can update the progress bar. Here is what method that creates the thread to upload the file. This is the method for the upload button

  1. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  2. up.upload(jTextField1.getText());
  3. upload = new UploadThread(up,jTextField1.getText());
  4. UploadThr = new Thread(upload);
  5. UploadThr.start();
  6. }

This is called in the Gui. I need to get the GUI responsive while it is uploading.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: Threads

 
0
  #2
May 7th, 2009
Essentially, you need to make sure that whatever call is blocking is actually inside the background thread. I don't know the library you're using, but I'm guessing that the call to up.upload() is blocking. (Put some logging on or run in the debugger to find out.)
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,634
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Threads

 
0
  #3
May 8th, 2009
I think that your main thread get blocked - My suggestion is an example:

  1. import java.awt.*;
  2. import java.io.*;
  3. import java.util.StringTokenizer;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6.  
  7. public class Test extends JFrame
  8. {
  9. JButton b1=new JButton("Start");
  10. JTextField t1=new JTextField("",20);
  11.  
  12. public Test() {
  13. super("Simple Program");
  14. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. getContentPane().setLayout(new FlowLayout());
  16.  
  17. getContentPane().add(t1);
  18. getContentPane().add(b1);
  19.  
  20. b1.addActionListener(new ActionListener() {
  21. public void actionPerformed(ActionEvent e) {
  22. try {
  23. new AnyAction(t1);
  24. }catch(Exception ex) { }
  25. }
  26. });
  27.  
  28. pack();
  29. setVisible(true);
  30. }
  31. public static void main(String[] args) {
  32. new Test();
  33. }
  34. }
  35. class AnyAction extends Thread
  36. {
  37. JTextField tx;
  38. int i=0;
  39. public AnyAction(JTextField t) {
  40. tx=t;
  41. start();
  42. }
  43. public void run() {
  44. while(true) {
  45. i++;
  46. tx.setText("" + i);
  47. }
  48. }
  49. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 32
Reputation: hardik.rajani is an unknown quantity at this point 
Solved Threads: 9
hardik.rajani's Avatar
hardik.rajani hardik.rajani is offline Offline
Light Poster

Re: Threads

 
0
  #4
May 13th, 2009
In the program you give there is no problem of GUI blocking. Clearify the problem.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,597
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Threads

 
0
  #5
May 13th, 2009
Originally Posted by evant8950 View Post
I'm working on a program to upload some files to an ftp. I created a thread the starts the upload when the upload button is pushed. When the upload button is pushed the GUI freezes because it is uploading. What change would I have to make to get the GUI responsive
If the problem is that the GUI is frozen temporarily while some other task is being completed, you need to use a SwingWorker thread. Run the task that is freezing the GUI in the SwingWorker thread, and your GUI will not be frozen anymore.

Other SwingWorker info: http://java.sun.com/docs/books/tutor...cy/simple.html

(But if the problem is simply that the GUI is permanently frozen, then you have another problem). In the scenario where you'd use a SwingWorker thread, it'd be because you are trying to complete some time consuming task on the EDT, and as a result, your GUI (which executes on the EDT) would have to wait for that task to finish before it can continue. So be careful because if, for some reason, your upload method is blocking & you put it in a SwingWorker thread, your GUI will work again. . but it won't have solved your problem (your upload method will still block, it just won't affect the GUI). Basically, use a SwingWorker thread if there is some time consuming task that freezes the GUI temporarily.
Last edited by BestJewSinceJC; May 13th, 2009 at 11:45 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC