Results 1 to 7 of 7
Thread: UI not updating
- 08-27-2012, 10:18 PM #1
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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
Any help is appreciated.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)); //} } });
-
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
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.Java Code:BackupHandler.download(filename);
Last edited by Fubarable; 08-27-2012 at 11:11 PM.
- 08-27-2012, 11:25 PM #3
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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.
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.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(); } } }
-
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.
-
Re: UI not updating
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.
- 08-28-2012, 12:00 AM #6
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
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?
-
Re: UI not updating
You don't have to make it threadable, but instead call it from within a SwingWorker. Let the SwingWorker do all the background threading for you.
Similar Threads
-
Updating JDK
By sandz24 in forum New To JavaReplies: 4Last Post: 01-20-2012, 03:34 AM -
Updating .jar through WinRAR?
By krazy in forum New To JavaReplies: 3Last Post: 08-17-2011, 07:05 AM -
Updating my gui
By mrx89_7 in forum New To JavaReplies: 4Last Post: 02-09-2011, 05:33 AM -
Updating JTabel
By drwk in forum New To JavaReplies: 2Last Post: 01-17-2010, 12:52 PM -
Updating my GUI
By Catkill in forum AWT / SwingReplies: 6Last Post: 09-01-2009, 05:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks