954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Threads

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

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


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

Thanks

functionalCode
Light Poster
32 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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.)

neilcoffey
Junior Poster in Training
53 posts since Dec 2008
Reputation Points: 120
Solved Threads: 7
 

I think that your main thread get blocked - My suggestion is an example:

import java.awt.*;
import java.io.*;
import java.util.StringTokenizer;
import javax.swing.*;
import java.awt.event.*;

public class Test extends JFrame
{
             JButton b1=new JButton("Start");
             JTextField t1=new JTextField("",20);
               
               public Test() {
                      super("Simple Program");
                      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      getContentPane().setLayout(new FlowLayout());

                      getContentPane().add(t1);
                      getContentPane().add(b1);

                      b1.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                 try {
                                         new AnyAction(t1);
                                       }catch(Exception ex) { }
                             }
                      });
 
                      pack();
                      setVisible(true); 
                }
	public static void main(String[] args) {
		 new Test();
	}
}
class AnyAction extends Thread
{
    JTextField tx;
     int i=0;   
    public AnyAction(JTextField t) {
       tx=t;
       start();          
    }
   public void run() {
          while(true) {
                i++;
                tx.setText("" + i);
          }
   }  
}
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

In the program you give there is no problem of GUI blocking. Clearify the problem.

hardik.rajani
Light Poster
41 posts since Apr 2009
Reputation Points: 10
Solved Threads: 9
 
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/tutorial/uiswing/concurrency/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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You