Results 1 to 7 of 7

Thread: UI not updating

  1. #1
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default UI not updating

    I am writing an application to upload/download a single file from a server. The problem is that my UI is not updating when I want it to. I realize the problem may be with the EDT, and I have utilized a SwingWorker to circumvent it. However, my UI still freezes up. Here is the code in question

    Java Code:
    btnDownload.addActionListener(
    				new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						//textArea_2.append("\nStarting Download");
    						worker = new SwingWorker(){
    							public Object construct(){
    								return getFileName(comboBox_1.getSelectedItem().toString());
    							}
    							
    						
    							
    						}; 
    						
    						
    						textArea_2.setText("\nStarting Download");
    						frame.repaint(); 
    						worker.start(); 
    						String filename = worker.get().toString(); 
    						BackupHandler.download(filename);
    						//ArrayList<String> backups = BackupHandler.getLocalFiles(); 
    						textArea_2.append("\nDownload Finished"); 
    						//for(int i = 0; i < backups.size(); i++){
    						//	textArea_3.append(backups.get(i)); 
    							
    						//}
    						
    					}
    				});
    Any help is appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: UI not updating

    Have you gone through the SwingWorker tutorial? I'm not sure what class you're using but it's not a javax.swing.SwingWorker since it does not have a doInBackground method, the method where you should be reading in your file. Please read the tutorial as it's all explained there.

    Also if the line
    Java Code:
    BackupHandler.download(filename);
    is where you actually download the file, please note that you're doing it on the Swing event thread, so no background threading appears to be done in your code.
    Last edited by Fubarable; 08-28-2012 at 12:11 AM.

  3. #3
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: UI not updating

    That fixed it. I didn't read that one but a third part site or something that was outdated. I can't believe I put the wrong method in the thread class. I must also be using a different Swing Worker class as I didn't import anything. Instead I just created a SwingWorker class file, and made an instance of that.

    Java Code:
    import javax.swing.SwingUtilities;
    
    /**
     * This is the 3rd version of SwingWorker (also known as
     * SwingWorker 3), an abstract class that you subclass to
     * perform GUI-related work in a dedicated thread.  For
     * instructions on using this class, see:
     * 
     * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
     *
     * Note that the API changed slightly in the 3rd version:
     * You must now invoke start() on the SwingWorker after
     * creating it.
     */
    public abstract class SwingWorker {
        private Object value;  // see getValue(), setValue()
        private Thread thread;
    
        /** 
         * Class to maintain reference to current worker thread
         * under separate synchronization control.
         */
        private static class ThreadVar {
            private Thread thread;
            ThreadVar(Thread t) { thread = t; }
            synchronized Thread get() { return thread; }
            synchronized void clear() { thread = null; }
        }
    
        private ThreadVar threadVar;
    
        /** 
         * Get the value produced by the worker thread, or null if it 
         * hasn't been constructed yet.
         */
        protected synchronized Object getValue() { 
            return value; 
        }
    
        /** 
         * Set the value produced by worker thread 
         */
        private synchronized void setValue(Object x) { 
            value = x; 
        }
    
        /** 
         * Compute the value to be returned by the <code>get</code> method. 
         */
        public abstract Object construct();
    
        /**
         * Called on the event dispatching thread (not on the worker thread)
         * after the <code>construct</code> method has returned.
         */
        public void finished() {
        }
    
        /**
         * A new method that interrupts the worker thread.  Call this method
         * to force the worker to stop what it's doing.
         */
        public void interrupt() {
            Thread t = threadVar.get();
            if (t != null) {
                t.interrupt();
            }
            threadVar.clear();
        }
    
        /**
         * Return the value created by the <code>construct</code> method.  
         * Returns null if either the constructing thread or the current
         * thread was interrupted before a value was produced.
         * 
         * @return the value created by the <code>construct</code> method
         */
        public Object get() {
            while (true) {  
                Thread t = threadVar.get();
                if (t == null) {
                    return getValue();
                }
                try {
                    t.join();
                }
                catch (InterruptedException e) {
                    Thread.currentThread().interrupt(); // propagate
                    return null;
                }
            }
        }
    
    
        /**
         * Start a thread that will call the <code>construct</code> method
         * and then exit.
         */
        public SwingWorker() {
            final Runnable doFinished = new Runnable() {
               public void run() { finished(); }
            };
    
            Runnable doConstruct = new Runnable() { 
                public void run() {
                    try {
                        setValue(construct());
                    }
                    finally {
                        threadVar.clear();
                    }
    
                    SwingUtilities.invokeLater(doFinished);
                }
            };
    
            Thread t = new Thread(doConstruct);
            threadVar = new ThreadVar(t);
        }
    
        /**
         * Start the worker thread.
         */
        public void start() {
            Thread t = threadVar.get();
            if (t != null) {
                t.start();
            }
        }
    }
    Should I separate all the code that does the download and uploading and put that into another class and make a thread of that class? It may be easier to do that because I have to pass in variables, and it's hard to do that using SwingWorker implemented like that. Thank you for pointing out that one stupid mistake.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: UI not updating

    Don't use that SwingWorker as that is likely an old version someone cooked up before the official SwingWorker became available in Java 5. Again, please read the tutorial as it's all explained there.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: UI not updating

    Quote Originally Posted by Wnt2bsleepin View Post
    Should I separate all the code that does the download and uploading and put that into another class and make a thread of that class? It may be easier to do that because I have to pass in variables, and it's hard to do that using SwingWorker implemented like that. Thank you for pointing out that one stupid mistake.
    Yes, by all means separate the code that does file I/O from the rest of your program and for many reasons. Again, read the SwingWorker tutorial and use the core Java SwingWorker. Often you'll pass variables into the SwingWorker by way of its constructors. You'll construct it only when you need it, and you can't re-use it.

  6. #6
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: UI not updating

    I have another class that actually does all the upload, downloading and the connecting. I am only making an instance of it and calling methods from the GUI class. Do you think it would be better to make the I/O class threadable instead of making a completely new class that would make an instance of the I/O class and call methods?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Updating JDK
    By sandz24 in forum New To Java
    Replies: 4
    Last Post: 01-20-2012, 04:34 AM
  2. Updating .jar through WinRAR?
    By krazy in forum New To Java
    Replies: 3
    Last Post: 08-17-2011, 08:05 AM
  3. Updating my gui
    By mrx89_7 in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 06:33 AM
  4. Updating JTabel
    By drwk in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 01:52 PM
  5. Updating my GUI
    By Catkill in forum AWT / Swing
    Replies: 6
    Last Post: 09-01-2009, 06:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •