| | |
Threads
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2009
Posts: 25
Reputation:
Solved Threads: 0
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
This is called in the Gui. I need to get the GUI responsive while it is uploading.
Thanks
java Syntax (Toggle Plain Text)
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
I think that your main thread get blocked - My suggestion is an example:
Java Syntax (Toggle Plain Text)
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); } } }
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Sep 2008
Posts: 1,599
Reputation:
Solved Threads: 202
•
•
•
•
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
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.
![]() |
Similar Threads
- I want google to index forum threads (Search Engine Optimization)
- new way the threads are timed (DaniWeb Community Feedback)
- IE won't let me access threads. Could someone email me a solution please? (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: A Node question
- Next Thread: outofbound error (arrays)
| Thread Tools | Search this Thread |
911 addball android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css database eclipse ee error event exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javadoc javafx javaprojects jni jpanel julia jvm key linux list loan loop machine map method methods migrate mobile netbeans newbie nls oracle output phone physics print problem program programming project radio recursion scanner screen server service set size sms socket software sort sql string swing textfield threads time transfer tree trolltech ubuntu utility windows






